Map with multiple inputs python

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

def cube(n):

    return n**3

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

def sum(a,b):

    return a+b

lst1=[2,4,6,8]

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

def Multiply(a,b,c):

    return a*b*c

lst1=[2,4,6,8,10,12,14,16]

lst2=[1,3,5,7,9,11,15]

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

def division(a,b):

    return a/b

lst=[2,4,6,8,10,12,14,16]

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]


Can Map function have more than 2 arguments?

We can pass multiple iterable arguments to map() function, in that case, the specified function must have that many arguments. The function will be applied to these iterable elements in parallel. With multiple iterable arguments, the map iterator stops when the shortest iterable is exhausted.

How do I map 2 values in Python?

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.

How do you pass multiple arguments on a map?

To pass multiple arguments to the map() function: Pass the handler function and the arguments to the functools. partial() method. Pass the result and the iterable to the map() function. The handler function will get called the arguments on each iteration.

How many arguments can map have?

The map function has two arguments (1) a function, and (2) an iterable.