List Comprehensions

List comprehensions are a unique way of creating a list with much little memory . It allows programmers to write multiple lines of code in a single line. This does make the code look clean however the overall readability of code reduces significantly .

Basically , if you ever want to append certain elements into a list , then that is where list comprehensions can come in handy. So instead of using the append() method along with a loop , you can use list comprehension . Let us understand this with the help of an example.

Say you want to append all the natural numbers from 1 to 50 to a list , using the knowledge we know , we’ll do something like this :

my_list = []  for x in range(1,51):    my_list.append(x)  print(my_list)

output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]

Now if we perform the the same operation with list comprehensions , our code will look something like this:

my_list = [ x for x in range(1,51) ]   print(my_list)

output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]

Let us take a few more examples to understand this better

print([ x for x in 'PY4U.ORG' ])

output:
['P', 'Y', '4', 'U', '.', 'N', 'E', 'T']

We can also add conditional statements in list comprehensions. In the code given below , we will be appending only even numbers in-between 1 to 100

print([ x for x in range(1,100) if x%2==0 ])

output:
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98]

We can also perform some operations on the element that is to be appended to the list. In the code given below , i will appending the squares of the even numbers in-between 1 to 100 to the list.

print([ x**2 for x in range(1,100) if x%2==0 ])

output:
[4, 16, 36, 64, 100, 144, 196, 256, 324, 400, 484, 576, 676, 784, 900, 1024, 1156, 1296, 1444, 1600, 1764, 1936, 2116, 2304, 2500, 2704, 2916, 3136, 3364, 3600, 3844, 4096, 4356, 4624, 4900, 5184, 5476, 5776, 6084, 6400, 6724, 7056, 7396, 7744, 8100, 8464, 8836, 9216, 9604]

It is also possible to use nested loops in list comprehensions .

print([ (x+y)*y for x in [1,2,3] for y in [1,2,3,4]  ])

output:
[2, 6, 12, 20, 3, 8, 15, 24, 4, 10, 18, 28]

List comprehensions does allow us to use if-else block , however i don’t recommend using it. It makes the code look far more complex then it actually needs to be. In the example given below i will be appending even numbers to the list and if the number is not even then i want to append ‘odd’ to the list.

print([ x if x%2==0 else 'odd' for x in range(1,100)  ])

output:
['odd', 2, 'odd', 4, 'odd', 6, 'odd', 8, 'odd', 10, 'odd', 12, 'odd', 14, 'odd', 16, 'odd', 18, 'odd', 20, 'odd', 22, 'odd', 24, 'odd', 26, 'odd', 28, 'odd', 30, 'odd', 32, 'odd', 34, 'odd', 36, 'odd', 38, 'odd', 40, 'odd', 42, 'odd', 44, 'odd', 46, 'odd', 48, 'odd', 50, 'odd', 52, 'odd', 54, 'odd', 56, 'odd', 58, 'odd', 60, 'odd', 62, 'odd', 64, 'odd', 66, 'odd', 68, 'odd', 70, 'odd', 72, 'odd', 74, 'odd', 76, 'odd', 78, 'odd', 80, 'odd', 82, 'odd', 84, 'odd', 86, 'odd', 88, 'odd', 90, 'odd', 92, 'odd', 94, 'odd', 96, 'odd', 98, 'odd']

Don’t get too depressed if you are not comfortable with list comprehensions. You can always go back to our dearly append() method .

list comprehensions does reduce the memory needed , however it has no impact on computational optimization of the code .