Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How do map, reduce and filter functions work in Python?
Python's map(), filter(), and reduce() functions bring functional programming concepts to Python. These functions accept a function and an iterable, applying the function to elements in different ways to produce results.
map() Function
The map() function applies a given function to each item in an iterable and returns a map object (iterator) with the results. Unlike reduce(), it operates independently on each item rather than producing a single cumulative result.
Syntax
map(function, iterable)
Parameters
function ? The function to apply to each element
iterable ? The sequence to iterate over (list, tuple, etc.)
Example
Here's how to multiply each number in a list by 3 using map() ?
# creating a function that returns multiplication result
def multiply_by_three(number):
# returning number after multiplying with 3
return number * 3
# input list
numbers = [1, 3, 5, 2, 6, 10]
# map() function applies the multiply_by_three function to each element
result = map(multiply_by_three, numbers)
# Converting map object to list and printing results
print("Multiplying list elements with 3:")
print(list(result))
Multiplying list elements with 3: [3, 9, 15, 6, 18, 30]
filter() Function
The filter() function creates an iterator from elements of an iterable for which the function returns True. It's used to filter out elements based on a condition.
Syntax
filter(function, iterable)
Parameters
function ? A function that returns True/False for filtering
iterable ? The sequence to filter
Example
Filter ages to find who is eligible for voting (18 and above) ?
# creating a function that checks voting eligibility
def is_voting_age(age):
# checking whether the age is greater than or equal to 18
return age >= 18
# input list of ages
ages = [3, 20, 18, 6, 14, 25, 19]
# Getting only values that are greater than or equal to 18
eligible_ages = filter(is_voting_age, ages)
# converting filter object to list
print("Eligible ages for voting:", list(eligible_ages))
Eligible ages for voting: [20, 18, 25, 19]
reduce() Function
The reduce() function applies a function cumulatively to items in an iterable, reducing it to a single value. It's available in the functools module.
Syntax
reduce(function, iterable)
Parameters
function ? A function that takes two arguments
iterable ? The sequence to reduce
Example
Calculate the sum of all numbers in a list using reduce() ?
# importing reduce() function from functools module
from functools import reduce
# function that returns the sum of two numbers
def add_numbers(x, y):
return x + y
# input list
numbers = [12, 4, 10, 15, 6, 5]
# Calculate sum using reduce() function
total = reduce(add_numbers, numbers)
print("The sum of all list items:", total)
The sum of all list items: 52
Comparison
| Function | Purpose | Returns | Example Use |
|---|---|---|---|
map() |
Transform each element | Iterator of results | Square all numbers |
filter() |
Select elements by condition | Iterator of filtered items | Find even numbers |
reduce() |
Combine all elements | Single accumulated value | Calculate product |
Conclusion
Use map() to transform each element, filter() to select elements based on conditions, and reduce() to combine all elements into a single value. These functions provide elegant alternatives to traditional loops for functional programming patterns.
---