Lambda, Map, Filter, and Reduce in Python

22 Mar 2026, Updated: 14 Jul 2026 3 min read
0
In Python, Lambda functions and higher-order functions like map(), filter(), and reduce() are key tools in functional programming.

They allow you to write clean, concise, and expressive code that focuses on what to do, rather than how to do it.
A higher-order function (HOF) is a function that either takes one or more functions as arguments or returns a function as its result.

Because Python treats functions as first-class citizens, they can be manipulated like any other object, such as integers or strings.

Lambda (Anonymous Functions)

A lambda function is a small, anonymous function — meaning it has no name. It is defined using the lambda keyword and can take any number of arguments, but it can only contain a single expression.
Syntax:
lambda arguments: expression
Example:
square = lambda x: x * x
print(square(5))  # Output: 25
Another example:
add = lambda a, b: a + b
print(add(3, 7))  # Output: 10
Lambda functions are commonly used when you need a simple function for a short duration — especially as arguments to other functions.

map()

The map() function applies a given function to each item in an iterable (like a list or tuple) and returns a map object (which can be converted into a list).
Syntax:
map(function, iterable)
Example:
numbers = [1, 2, 3, 4, 5]
squared = map(lambda x: x**2, numbers)
print(list(squared))  # Output: [1, 4, 9, 16, 25]
In this example, the lambda function squares each element of the list.

filter()

The filter() function filters elements from an iterable based on a condition provided by a function. It returns only those elements for which the function returns True.
Syntax:
filter(function, iterable)
Example:
numbers = [10, 15, 20, 25, 30]
even_numbers = filter(lambda x: x % 2 == 0, numbers)
print(list(even_numbers))  # Output: [10, 20, 30]
Here, the lambda function checks if each number is even, and filter() returns only those that satisfy the condition.

reduce()

The reduce() function (available in the functools module) repeatedly applies a function to the items of a sequence, reducing it to a single cumulative value.
Syntax:
from functools import reduce
reduce(function, iterable)
Example:
from functools import reduce

numbers = [1, 2, 3, 4, 5]
product = reduce(lambda x, y: x * y, numbers)
print(product)  # Output: 120
Here, reduce() multiplies all elements together: (((1 * 2) * 3) * 4) * 5 = 120

Summary

Lambda expressions and higher-order functions such as map(), filter(), and reduce() provide elegant and powerful ways to process data in Python.

They reduce boilerplate code, eliminate unnecessary loops, and make data transformations and aggregations more concise, expressive, and easier to read.
Concept Description
lambda Creates small, anonymous functions for short, one-line operations.
map() Applies a function to every element in an iterable and returns an iterator.
filter() Returns only the elements that satisfy a specified condition.
reduce() Combines all elements of an iterable into a single result.
functools Provides reduce() along with several other utilities for functional programming.
In the next article, we'll explore Modules and Packages and learn how to organize Python code into reusable, maintainable, and scalable applications.
Nagesh Chauhan

Nagesh Chauhan

Principal Software Engineer • Java • Python • Distributed Systems • AI/ML

Principal Software Engineer with 14+ years of experience designing and delivering large-scale distributed systems, cloud-native applications, and AI-powered platforms.

Passionate about solving complex engineering problems using strong data structures and algorithms, along with expertise in Java, Spring Boot, Python, System Design, Microservices, Cloud, Kafka, Elasticsearch, and Generative AI.

Share this Article

💬 Comments

Join the Discussion