What is a list compression in Python?

Rainer Grimm r.grimm at science-computing.de
Wed Jan 20 07:39:11 CET 2010


Hallo,
you can also look at list comprehension as syntactic sugar for the
functions map and filter. The two functions from the functional world
can be expressed in a comprehensive way with list comprehension.
>>> [x**2 for x in range(10) ] == map ( lambda x: x*x, range(10))
True
>>> [ x for x in range(10) if x%2 == 0 ] == filter ( lambda x: x%2 == 0 , range(10))
True

Greetings



More information about the Python-list mailing list