How to Pass Multiple Arguments to map()
in Python
The map()
function in Python applies a given function to each item of an iterable (like a list, tuple, etc.). While map()
is often used with a single iterable, you can also pass multiple iterables to it.
This guide explains how to use map()
with multiple iterables, and when list comprehensions might be a better alternative.
map()
with Multiple Iterables (The Standard Approach)
The map()
function can accept multiple iterables. If you provide multiple iterables, the function you pass to map()
must accept the same number of arguments as there are iterables. map()
will then "zip" the iterables together, passing corresponding elements from each iterable as arguments to your function.
def multiply(a, b):
return a * b
list_1 = [1, 2, 3, 4, 5]
list_2 = [6, 7, 8, 9, 10]
new_list = list(map(multiply, list_1, list_2)) # map() with two lists
print(new_list) # Output: [6, 14, 24, 36, 50]
map(multiply, list_1, list_2)
: This calls themultiply
function with corresponding elements fromlist_1
andlist_2
:- First call:
multiply(1, 6)
(result: 6) - Second call:
multiply(2, 7)
(result: 14) - Third call:
multiply(3, 8)
(result: 24) - ... and so on.
- First call:
list(...)
: Themap()
function returns a map object (which is an iterator). We uselist()
to convert it to a list so we can see the results.
You can extend this to any number of iterables, as long as your function accepts the corresponding number of arguments:
def multiply(a, b, c):
return a * b * c
list_1 = [1, 2, 3, 4, 5]
list_2 = [6, 7, 8, 9, 10]
list_3 = [1, 2, 3, 4, 5]
new_list = list(map(multiply, list_1, list_2, list_3))
print(new_list) # Output: [6, 28, 72, 144, 250]
Using List Comprehensions (Often Preferred)
For many cases, a list comprehension is more readable and often more efficient than using map()
:
def example_func(item, extra_arg):
print(item, extra_arg)
return item + extra_arg
my_list = [1, 2, 3, 4]
my_arg = 100
new_list = [example_func(item, my_arg) for item in my_list]
print(new_list) # Output: [101, 102, 103, 104]
- List comprehensions are often preferred over
map()
because they offer a more concise and readable way to achieve the same result.
Using functools.partial
(for Fixed Arguments)
If you have a function that takes multiple arguments, but you want to use map()
with only one iterable, and the other arguments are fixed, you can use functools.partial
:
from functools import partial
my_list = [1, 2, 3, 4]
def example_func(item, extra_arg):
print(item, extra_arg)
return item + extra_arg
my_arg = 100
# Create a new function that "pre-fills" extra_arg
new_func = partial(example_func, extra_arg=my_arg) # Keyword arguments are clearer
new_list = list(map(new_func, my_list))
print(new_list) # Output: [101, 102, 103, 104]
partial(example_func, extra_arg=my_arg)
: This creates a new function (new_func
) that's likeexample_func
, but withextra_arg
already set tomy_arg
. So,new_func
only takes one argument (item
).map(new_func, my_list)
: Now you can usemap()
with your new, single-argument function.
functools.partial
is very useful for creating specialized versions of more general functions.
Using itertools.repeat
(for a Single Repeated Argument)
If you want to apply a function that takes multiple arguments, and all but one of those arguments should be the same value for every call, use itertools.repeat
:
from itertools import repeat
def multiply(a, b):
return a * b
list_1 = [1, 2, 3, 4, 5]
new_list = list(map(multiply, list_1, repeat(5))) # Multiply each element by 5
print(new_list) # Output: [5, 10, 15, 20, 25]
repeat(5)
: Creates an iterator that returns the value5
endlessly.map()
will keep pulling values from this iterator until the shortest iterable is exhausted (in this case,list_1
).