A list comprehension is a convenient method to create lists in Python.
a = [ i for i in range( 5 ) ] # [0, 1, 2, 3, 4] a = [ i for i in range( 5 ) if 0 == ( i % 2 ) ] # [0, 2, 4] # In general any sequence of for and if a = [ i for ... if ... for ... for ... if ... ]
a = [ i for i in range( 5 ) if 0 == ( i % 2 ) ]
# ... this is the same as ...
a = []
for i in range( 5 ):
if 0 == ( i % 2 ):
a.append( i )
Tried with: Python 3.2
[...] generator expression is very similar to list comprehension in Python. The advantage of the generator expression over list comprehension is that the resulting [...]