First page Back Continue Last page Overview Graphics

List Comprehension

>>> temp = (36.5, 37, 37.5,39, 29.8, 27.3, 25.9)

We can write

>>> [ 1.8*t+32 for t in temp]

[97.7, 98.60000000000001, 99.5, 102.2, 85.64, 81.14, 78.62]

instead of

>>> list(map(lambda t: 1.8*t+32, temp))

>>> list(filter(lambda t: t>30, temp))

[36.5, 37, 37.5, 39]

can be replaced by:

>>> [ t for t in temp if t>=30 ]

[36.5, 37, 37.5, 39]

Replacing filter and map:

>>> [ 1.8*t+32 for t in temp if t>=30 ]

[97.7, 98.60000000000001, 99.5, 102.2]

instead of

>>> list(map(lambda t: 1.8*t+32, filter(lambda t: t>30, temp)))

[97.7, 98.60000000000001, 99.5, 102.2]