Python – pass multiple arguments to map function

• Last Updated : 23 Jun, 2020

The map() function is a built-in function in Python, which applies a given function to each item of iterable (like list, tuple etc) and returns a list of results or map object.

Syntax :

map(funct, iterbl)

Parameters :

funct : The function which is going to execute for each iterable
iterbl : A sequence or collection of iterable objects which is to be mapped

Note :

1.  You can pass as many iterable as you like to map() function.
2. Ensure that function has one parameter for each iterable.

Example :

Python3

 # Python program to show working# of map() function  # Return cube of ndef cube(n):    return n**3  # Taking list as iteratorevennum = [2,4,6,8]res = map(cube,evennum)print(list(res))

Output :

[8, 64, 216, 512]

Passing Multiple Arguments to map() function

We can pass multiple iterable arguments to map() function. For this certain rules must be followed-

• Suppose we pass n iterable to map(), then the given function should have n number of arguments.
• These iterable arguments must be applied on given function in parallel.
• In multiple iterable arguments, when shortest iterable is drained, the map iterator will stop.
• But in case of Python 2, the map iterator will stop when longest sequence is finished.

CODE 1 : Passing two lists and ‘sum’ function to map().

Approach:

• Define a function sum, which returns sum of two numbers.
• Declaring and initializing lst1 and lst2.
• Passing sum function, lst1 and lst2 to map().
• The element at index 0 from both the list will pass on as argument to sum function and their sum will be returned.
• This loop continues till elements of one list get exhausted.
• The result will be stored in result list.

Python3

 # Python program to demonstrate# passing of multiple iterable arguments to map()# using 2 lists  # Function which return sum of 2 numbersdef sum(a,b):    return a+b  # list 1lst1=[2,4,6,8]  # list 2lst2=[1,3,5,7,9]    result=list(map(sum,lst1,lst2))print(result)

Output :

[3, 7, 11, 15]

CODE 2 :  Passing three lists and ‘Multiply’ function to map().

Approach:

• Define a function Multiply, which returns product of three numbers.
• Declaring and initializing lst1, lst2 and lst3.
• Passing Multiply function, lst1, lst2 and lst3 to map().
• The element at index 0 from all three lists will pass on as argument to Multiply function and their product will be returned.
• This loop continues till elements of one list get exhausted.
• The result will be stored in result list.

Python3

 # Python program to demonstrate# passing of multiple iterable arguments to map()# using 3 lists  # Function which return product of 2 numbersdef Multiply(a,b,c):    return a*b*c  # list 1lst1=[2,4,6,8,10,12,14,16]  # list 2lst2=[1,3,5,7,9,11,15]  #list 3lst3=[2,3,5,7,11,13,17]    result=list(map(Multiply,lst1,lst2,lst3))print(result)

Output :

[4, 36, 150, 392, 990, 1716, 3570]

CODE 3 : Passing ‘division’ function, one list and one tuple to map().

Approach :

• Defining and initializing list and tuple.
• Defining division function which performs division of two number.
• Passing lst, tup and division function to map().
• The element at index 0 from list and tuple will pass on as argument to division function and their quotient will be returned.
• This loop continues till elements of either list or tuple get exhausted.
• The result will be stored in result list.

Python3

 # Python program to demonstrate# passing of multiple iterable arguments to map()# using list and tuple  # Function which perform division of 2 numbersdef division(a,b):    return a/b  # list lst=[2,4,6,8,10,12,14,16]  # tupletup=(2,3,5,7,9,11)  result=list(map(division,lst,tup))print(result)

Output :

[1.0, 1.3333333333333333, 1.2, 1.1428571428571428, 1.1111111111111112, 1.0909090909090908]

My Personal Notes arrow_drop_up