Python Tip of the Day: Functional Programming with map(), filter(), and reduce()

Functional Programming with map(), filter(), and reduce()

Author: Jeremy Morgan
Published: November 18, 2024


Coding with AI

I wrote a book! Check out A Quick Guide to Coding with AI.
Become a super programmer!
Learn how to use Generative AI coding tools as a force multiplier for your career.


Process data in a functional style—clean and expressive.

# Using map, filter, and reduce
from functools import reduce

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

# Double each number
doubled = list(map(lambda x: x * 2, numbers))
print(doubled)  # Output: [2, 4, 6, 8, 10]

# Filter out even numbers
evens = list(filter(lambda x: x % 2 == 0, numbers))
print(evens)  # Output: [2, 4]

# Sum all numbers
total = reduce(lambda x, y: x + y, numbers)
print(total)  # Output: 15

“Python Tip of the Day: Functional Programming with map(), filter(), and reduce()”


The Python Tip of the Day is a daily series published in the month of November. The tips are designed to help you become a better Python programmer. I post tips like this and more every single day on X. Let’s connect!


Coding with AI

I wrote a book! Check out A Quick Guide to Coding with AI.
Become a super programmer!
Learn how to use Generative AI coding tools as a force multiplier for your career.