Why python list comprehension are awesome

I can’t think of how many times have just wondered about how awesome the python’s list comprehensions are. I found them extremely handy while I was preprocessing data for my data visualisation project.

Let me share with you some examples, general syntax and some nifty tricks.

Simple list comprehension example

Create list of numeric values ( this example is quite useless but illustrative for beginning )

1

[iforiinxrange(4)]

1

[0,1,2,3]

The code is interpreted as : “run interation for i, and in each iteration, append i to the list”. Therefore we can go crazy with how we are iterating. We can iterate through some list or generator. Anything that’s iterable.

Process iteration result before appending into list

We can however play much more with the results of the iterations which we put into the created list. For example we may want to create list of powers of 2. We can simply do

1

[2**iforiinxrange(12)]

1

[1,2,4,8,16,32,64,128,256,512,1024,2048]

Or even call some function to process the result of iteration somehow.

1

[len(str(2**i))foriinxrange(12)]

1

[1,1,1,1,2,2,2,3,3,3,4,4]

This example gives us array of numbers saying how many characters powers of 2 got.

Use custom generators with list comprenesions

Using generators we can make our list comprehension more readable. We can define custom generator and use it

1

2

3

4

5

6

7

defPowerOfTwoGenerator(upTo):

i=1

while(i<upTo):

yield2**i

i+=1

[iforiinPowerOfTwoGenerator(12)]

1

[2,4,8,16,32,64,128,256,512,1024,2048]

Use conditions for processing iteration result

When we receive the result from the iteration, we can do more complex things with it thanks to conditions. Like this :

1

[(aifa%2==0elsea*1000)forain[1,2,3,4,5,6,7]]

1

[1000,2,3000,4,5000,6,7000]

Use conditions to filter out appended iteration results

When the condition in previous example is evaluated, it’s for sure that >SOME< value will be added to the list. However, sometimes we might want to filter out values we receieve from generator or iterated list. We can filter out even values

1

[aforain[1,2,3,4,5,6,7]ifa%2==0]

1

[2,4,6]

Iterate multiple variables

1

[[x,y]forxin[1,2,3]foryin[1,2]]

1

[[1,1],[1,2],[2,1],[2,2],[3,1],[3,2]]

Note that we just constructed the list of lists. Similarly we can also construct objects or dictionary

1

[{str(x):y}forxin[1,2,3]foryin[1,2]]

1

[{'1':1},{'1':2},{'2':1},{'2':2},{'3':1},{'3':2}]

Trick: Nested list comprehension : Flattening list of lists

We may have nested lists we want to flat out. Let’s have

1

[[1,2,3],[1337,666,42],[7,8,9]]

To flat out, we can use nested list comprehension.

1

[item forsublist in[[1,2,3],[1337,666,42],[7,8,9]]foritem insublist]

1

[1,2,3,1337,666,42,7,8,9]

The syntax may be a bit intimidating on the first glance. If you look closer however makes perfect sence.
I’ll add parenthesis to make it little bit more readable

1

[(item)forsublist in[[1,2,3],[1337,666,42],[7,8,9]]foritem insublist]

Now we need to realise that each element of iterated list is also list. This list element we called sublist. And then we use second iteration to iterate each element of this sublist : for item in sublist.
This is actually works pretty much the same as previous example