Friday, December 27, 2024
Google search engine
HomeLanguagesPython – pass multiple arguments to map function

Python – pass multiple arguments to map function

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( function, iterable )

Parameters :

  • function: The function which is going to execute for each iterable
  • iterable: 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 in Python.
  2. Ensure that function has one parameter for each iterable.

Example :

Python3




# Python program to show working
# of map() function
 
# Return cube of n
def cube(n):
    return n**3
 
# Taking list as iterator
evennum = [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.

Passing two lists and ‘sum’ function to map()

Define a function sum, which returns sum of two numbers. Declaring and initializing lst1 and lst2. Passing sum function, list1 and list2 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 numbers
def sum(a,b):
    return a+b
 
# list 1
lst1=[2,4,6,8]
 
# list 2
lst2=[1,3,5,7,9]
 
 
result=list(map(sum,lst1,lst2))
print(result)


Output :

[3, 7, 11, 15]

Passing three lists and ‘Multiply’ function to map()

Define a function Multiply, which returns product of three numbers. Declaring and initializing lst1, lst2 and lst3. Passing Multiply function, list1, list2 and list3 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 numbers
def Multiply(a,b,c):
    return a*b*c
 
# list 1
lst1=[2,4,6,8,10,12,14,16]
 
# list 2
lst2=[1,3,5,7,9,11,15]
 
#list 3
lst3=[2,3,5,7,11,13,17]
 
 
result=list(map(Multiply,lst1,lst2,lst3))
print(result)


Output :

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

Passing ‘division’ function, one list and one tuple to map()

Defining and initializing list and tuple. Defining division function which performs division of two number. Passing list, 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 numbers
def division(a,b):
    return a/b
 
# list
lst=[2,4,6,8,10,12,14,16]
 
# tuple
tup=(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]

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments