Comprehension
List comprehension
>>> [i for i in range(5)]
[0, 1, 2, 3, 4]
Tip
There is a shortcut for constructing a list in Construct Iterable.
Nested list comprehension
>>> [(i,j) for i in range(5) for j in range(i) ]
[(1, 0),
(2, 0),
(2, 1),
(3, 0),
(3, 1),
(3, 2),
(4, 0),
(4, 1),
(4, 2),
(4, 3)]
>>> [j for i in range(5) for j in range(i)]
[0, 0, 1, 0, 1, 2, 0, 1, 2, 3]
Key point to remember here (which took me a long time to realize) is that the order of the for statements is to be written in the order you’d expect them to be written in a standard for loop, from the outside inwards.
Dict comprehension
>>> ls = [1,2,3]
>>> d = {i: i+1 for i in ls}
>>> d
{1: 2, 2: 3, 3: 4}
Set comprehension
>>> ls = [1, 2, 2, 3]
>>> s = {i for i in ls}
>>> s
{1, 2, 3}
Generator comprehension
>>> ls = [1, 2, 2, 3]
>>> s = (i for i in ls)
>>> list(s)
[1, 2, 3]