SyntaxStudy
Sign Up
Python Comprehensions vs map/filter
Python Intermediate 3 min read

Comprehensions vs map/filter

Comprehensions vs map/filter

Comprehensions are often more readable than map() and filter(). Both are valid, but comprehensions are generally preferred in modern Python.

Example
# map + filter style
squares = list(map(lambda x: x**2, filter(lambda x: x % 2 == 0, range(10))))

# comprehension style
squares = [x**2 for x in range(10) if x % 2 == 0]
Pro Tip

Comprehensions are usually more Pythonic and readable than map/filter chains.