Nearby lessons
74 of 159Python - map() and filter() Functions
📌 What You Will Learn
- Describe the purpose of map() and filter()
- Use functions and lambda expressions with both tools
- Convert lazy results to lists
- Chain transformations and filtering
- Choose a readable alternative when a comprehension is clearer
Functional transformations
map() and filter() accept a callable and an iterable. They are useful for concise data-processing pipelines.
map() syntax
Use map(function, iterable). The function is called once for each item.
Mapping with a named function
Mapping with lambda
filter() syntax
Use filter(predicate, iterable). Items are included when the predicate returns a truthy value.
Filtering even numbers
Mapping and filtering together
Lazy results and readability
In Python 3, neither function creates a list automatically. Use list() for display or repeated access. For simple expressions, a list comprehension may be easier for readers to understand.
📝 Key Takeaways
- map() applies a function to every item
- filter() keeps items for which a predicate is true
- Both return lazy iterators in Python 3
- list() materializes their results
- Comprehensions can express simple map/filter operations clearly
🧠 Test Your Knowledge
8 QuestionsProgress: 0 / 8