filter() where list comprehension is possible¶
For simple transformations that can be expressed as a list comprehension, use list comprehensions over
filter() for expressions that are too long or complicated to express with a list comprehension. Although a
filter() expression may be functionally equivalent to a list comprehension, the list comprehension is generally more concise and easier to read.
The code below defines a list, and then uses
map() to create a second list which is just the doubles of each value from the first list.
values = [1, 2, 3] doubles = map(lambda x: x * 2, values)
Use list comprehension instead of
In the modified code below, the code uses a list comprehension to generate the second list containing the doubled values from the first list. Although this is functionally equivalent to the first code, the list comprehension is generally agreed to be more concise and easier to read.
values = [1, 2, 3] doubles = [x * 2 for x in values]