Nearby lessons

74 of 159

Python - 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

Loading editor…

Mapping with lambda

Loading editor…

filter() syntax

Use filter(predicate, iterable). Items are included when the predicate returns a truthy value.

Filtering even numbers

Loading editor…

Mapping and filtering together

Loading editor…

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 Questions
Progress: 0 / 8

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.