Python map(), filter(), reduce() functions(with examples)

Python map(), filter(), reduce() functions

Hello Techies,

This tutorial is based on the Python map(), filter(), reduce() functions, In this blog, you will learn How to use the map, filter, and reduce functions with the help of multiple examples.

Below are the points we are covering in this blog:

  • Python map() Function
  • Python filter() Function
  • Python reduce() Function

Python map() Function

The map() function performs the specified function for each iterable object(list, tuple etc.) and returns a list of results. The item is sent to the function as a parameter. In Python, map is a built-in function.

Syntax of map() function

map(function_name, sequence)

map() function takes 2 parameters,

  • The function will apply to all items available in the iterator.
  • Sequence can be list, tuple, etc. You can pass multiple sequence to the map() function.

The map() function will apply the given function to all elements inside the iterator and returns a list of the results.

How map() function works in Python?

The map() function takes two inputs as a parameter that is function and sequence. The function assigned to the map() is a normal function and it will iterate on all the values in the given iterable object.

Let’s check with some examples how the map() function works in Python.

Example 1: How do you use the map function in Python?

def sum(n):
    return n + n

my_list = [2, 3, 4, 5, 6, 7, 8, 9]
updated_list = map(sum, my_list)
print(list(updated_list))

Output is:

[4, 6, 8, 10, 12, 14, 16, 18]

Example 2: map() function using lambda function

result = map(lambda x: x + x, [2, 3, 4, 5, 6, 7, 8, 9])
print(list(result))

Output is:

[4, 6, 8, 10, 12, 14, 16, 18]

Python filter() Function

The Python filter() function returns a iterator where the items are filtered by the function to check whether the item is accepted or not.

Syntax of Python filter() function

map(function_name, sequence)

filter() function takes 2 parameters:

  • The function will apply to all items available in the iterator and it will checks whether the elements return true or false.
  • Sequence can be list, tuple, etc.

Python filter() function returns an iterator.

How Python filter() function works

Let’s check with some examples how the filter() function works in Python.

Example 1: How do you use the filter function in Python?

def my_func(n):
    if n >= 0:
        return True
    else:
        return False

my_list = [2, -3, 4, -5, 6, -7, 8, 9]
result = filter(my_func, my_list)

for i in result:
    print(i)

Output is:

2
4
6
8
9

Example 2: filter() function using lambda function

x = filter(lambda y: (y >= 0), [2, -3, 4, -5, 6, -7, 8, 9])
print(list(x))

Output is:

[2, 4, 6, 8, 9]

Python reduce() Function

Python reduce() function is used to reduce a sequence of elements to a single value by processing the elements according to a function provided. It returns a single value.

This function is a part of functools module so you need to import it before you can use it.

Syntax of Python reduce() function

from functools import reduce
reduce(function_name, sequence)

Python reduce() function takes 2 parameters,

  • Initially, the function is called with the first two items in the sequence and the result is returned.
  • The function is then repeated with the result obtained in step 1 and the next value in the sequence. This process keeps repeating until there are items in the sequence.
  • Sequence can be list, tuple, etc.

filter() function returns a single value.

How Python reduce() function works

Let’s check with some examples how the reduce() function works in Python.

Example 1: How do you use the reduce function in Python?

from functools import reduce

def my_func(x, y):
    return x + y

print(reduce(my_func, [1, 2, 3, 45]))

Output is:

51

Example 2: reduce() function using lambda function

from functools import reduce

result = reduce((lambda x, y: x + y), [1, 2, 3, 45])
print(result)

Output is:

51

FAQ on map, filter, reduce Functions in Python

  1. What is the difference between filter, map and reduce in Python?

    The map creates new arrays by individually converting each element in the array. The filter creates a new array by removing unrelated elements. reduce, on the other hand, takes all the elements in the array and reduces them into a single value.

  2. How do I use the map in Python 3?

    The map() function takes two inputs as a parameter that is function and sequence. The function assigned to the map() is a normal function and it will iterate on all the values in the given iterable object.

  3. Is filter a built-in function in Python?

    Yes, The Python’s in-built filter() function returns a iterator where the items are filtered by the function to check whether the item is accepted or not.


I hope you understand the concept of the Python set, filter, and reduce function. If you have any doubt please comment below.

Leave a Comment