Codectionary / Developer documentation / Python

map() and filter()

map() and filter() are built-in functions for applying a function across an iterable in a functional programming style. map() transforms every item by applying a function to each one, returning a lazy map object. filter() keeps only the items for which a function returns True, discarding the rest. Both are often used with lambda functions and, in modern Python, are frequently replaced by the more readable list comprehension syntax.

Syntax

map(function, iterable)\nfilter(function, iterable)

Examples

Using map()

Applying a transformation to every item in an iterable.

numbers = [1, 2, 3, 4, 5]

doubled = map(lambda n: n * 2, numbers)
print(list(doubled))  # [2, 4, 6, 8, 10]

# map() also works with named functions
def celsius_to_fahrenheit(c):
    return c * 9 / 5 + 32

temps_c = [0, 20, 37, 100]
temps_f = list(map(celsius_to_fahrenheit, temps_c))
print(temps_f)  # [32.0, 68.0, 98.6, 212.0]

Using filter()

Keeping only items that satisfy a condition.

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

evens = filter(lambda n: n % 2 == 0, numbers)
print(list(evens))  # [2, 4, 6, 8, 10]

words = ["hi", "hello", "hey", "greetings"]
long_words = list(filter(lambda w: len(w) > 3, words))
print(long_words)  # ['hello', 'greetings']

Combining map() and filter()

Chaining the two together to filter, then transform (or vice versa).

numbers = range(1, 11)

evens = filter(lambda n: n % 2 == 0, numbers)
squared_evens = map(lambda n: n ** 2, evens)

print(list(squared_evens))  # [4, 16, 36, 64, 100]

List Comprehension Equivalent

The same logic expressed with a list comprehension, which many developers find more readable.

numbers = range(1, 11)

# map + filter combined
result1 = list(map(lambda n: n ** 2, filter(lambda n: n % 2 == 0, numbers)))

# Equivalent, often more readable, list comprehension
result2 = [n ** 2 for n in numbers if n % 2 == 0]

print(result1 == result2)  # True

Best practices

  • Consider a list comprehension instead of map()/filter() with a lambda - it is often more readable in Python, though both are valid
  • Remember map() and filter() return lazy iterator objects, not lists - wrap with list() if you need to see or index the results directly
  • Use map() with a named function (not just a lambda) when the transformation logic is complex or reused elsewhere
  • Chain filter() before map() when you need to both narrow down and transform a collection

At a glance

Purpose
Scripting and general-purpose applications
File extension
.py
Runs in
Python interpreter
Usually used with
Python standard library and packages

Specifications & further reading

Related Python documentation

def
The def keyword in Python is used to define functions - reusable blocks of code that perform specific tasks. Functions are fundamental to organizing code, avoiding repetition, and making programs more maintainable. A function can accept inputs (parameters), perform operations, and return outputs. Functions are one of the core building blocks of clean, modular Python code.
lambda
A lambda is a small, anonymous, single-expression function, created without the def keyword or a name. Lambdas are restricted to a single expression whose result is automatically returned - they can't contain multiple statements or assignments. They're most useful as short, throwaway functions passed as arguments to other functions like sorted(), map(), and filter().
return
The return statement exits a function immediately and optionally sends a value back to the caller. A function can return any type - including multiple values as a tuple - or nothing at all, in which case it implicitly returns None. Once return executes, no further code in the function runs, making it useful for early exits as well as producing a final result.
*args and **kwargs
*args and **kwargs let a function accept a variable number of arguments. *args collects any extra positional arguments into a tuple, while **kwargs collects extra keyword arguments into a dictionary. The names 'args' and 'kwargs' are just convention - what matters is the * and ** prefixes. This pattern is common in wrapper functions, decorators, and APIs that need to stay flexible about their inputs.