All Courses

WHAT ARE MAP, FILTER AND REDUCE FUNCTIONS IN PYTHON…

Sulochana Kamshetty

2 years ago

map function
Table of Content
  • What is map()function in python
  • The filter() Function
  • Output
  • Explanation
  • The reduce() Function
  • output
Welcome to the new article from insideAIML which gives the complete knowledge or clear understanding about these above-mentioned topics.. those are
  • Map
  • Filter
  • Reduce
Let us understand these topics individually with the help of examples..
Map, Filter, and Reduce are language of functional programming. They allow the programmer (you) to write simpler, shorter code which is easy to understand and more feasible to code..

What is map()function in python:-

         This simple map() function allows the programmer to acces for many additional functions with simple codes which are iterables as many times we apply it on..
Where func is the function on which each element is iterables (as many as they are) would be applied on.
The difference of python programmed which is In Python2 version, the map() function returns a list. But In Python 3 version,  the function returns a map object which is a generator object. To get the result as a list, the built-in list() function can be called on the map object. i.e. list(map(func, *iterables))
The number of arguments to function  must be the number of iterables listed.
Lets see how is this map function rules with the codes…
my_games= [‘hockey’,’base ball’,’cricket’,’relayrace’]

uppered_games = []
the above created var with list of games is been stored in variable my_games..
for games in my_games:

    games_ = games.upper()

    uppered_games.append(games_)
output:- ['HOCKEY', 'BASE BALL', 'CRICKET', 'RELAYRACE']
Exaplaination:- the above for loop condition created a list of games and converted all the characters into
Capital letters using simple for loop condition..
We can pass as many iterable objects as we want after passing the function we want to use:
# lets understand with more additional functions without using lambdas and using def function
def starts_with_A(s):

    return s[0] == "A"

fruit = ["Apple",
"Banana", "Pear", "Apricot", "Orange"]

map_object =
map(starts_with_A, fruit)

print(list(map_object))
output: [True, False, False, True, False]
Explanation:- the above code we performed created the variable of name  fruits in which list of  fruits with different characters are stored with the def function and the condition commanded to give the output with the list of items which are contained with character A should give the output with conditional statements with either True or False statements so the above output provided with the results.. we even can perform the same thing using lambda functions too..
The filter() Function
Just like to map(), filter() takes a function object and an iterable and creates a new list.
 Filter() name it self suggest that it gives the only elements which passed returns the value, and satisfies the certain conditions.
Filter Function
My_List  = list(range(50))print([i for i in lst],end=",")
output:-  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
Explanation:- the filter function create the variable name my_list in which commanded condition created to give the all possible outputs range of 0-50 values  
Now, suppose we want to take only the odd numbers from this list, or more clearly, we want to filter all   the odd numbers from this list.
We first need the condition of how we will identify whether the number is odd or not.
Let’s create a function odd_func, which will return true or false based on if the number is odd or not.
The function passed to filter function should return boolean:
def is_odd(num):   

  return num%2!=0
# To use it with a list for filter, we will pass it as an argument in the
filter function.

filter_lst = filter(is_odd,lst)

print(type(filter_lst))
#output:- 
We can convert this filter obj to list by simply passing it to list function.
filter_lst = list(filter_lst)
The reduce() Function:-
Since we understood, about map, filter & its functions let’s see about reduce () function and its uses
So reduce function works quite differently than map () and filter () functions, it will give single value as an output, in-fact it doesn’t gives the list based output..
These all different functools module can be found in python3, isn’t a built-in function anymore
Below is the syntax of reduce function.
reduce(function, sequence[, initial])
reduce()  functions work taking two arguments as function and sequence and passed first two items in the sequence nd then gives the output in the form of sequence …
Let's understand its coding part and its output  
# Note :- The coding part is in applied in python 3
from functools import reduce

def
  do_sum(x1, x2):
    return x1 + x2
reduce(do_sum,
  [1, 2, 3,4])
output:- 
10

For more such blogs/courses on data science, machine learning, artificial intelligence and emerging new technologies do visit us at InsideAIML.
Thanks for reading…
Happy Learning…

Submit Review