Last updated: Apr 8, 2024
Reading timeยท3 min
You can pass a function, followed by multiple iterables to the map()
function.
The map()
function will call the supplied function with an item from each
iterable.
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)) print(new_list) # ๐๏ธ [6, 14, 24, 36, 50]
The map() function takes a function and one or more iterables as arguments and calls the function with each item of the iterables.
The map()
function first called the multiply()
function with the numbers 1
and 6, then with the numbers 2 and 7, etc.
You can use this approach with as many iterables as necessary.
However, make sure the function you pass to map
takes exactly as many
arguments as there are iterables.
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) # ๐๏ธ [6, 28, 72, 144, 250]
Notice that the multiply
function now takes 3 arguments.
It gets called with 1 item from each iterable.
The map()
function first calls multiply
with 1, 6 and 1, then with 2, 7 and
2, etc.
This is a three-step process:
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) # ๐๏ธ [101, 102, 103, 104]
List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.
The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.
Instead of using map()
, in most cases, you can just use a list comprehension
and make your code a bit easier to read.
This is a three-step process:
functools.partial()
method.map()
function.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 new_list = list( map(partial(example_func, extra_arg=my_arg), my_list) ) print(new_list) # ๐๏ธ [101, 102, 103, 104]
We used the
functools.partial()
method to pass multiple arguments to the map()
function.
The method takes a function, positional and keyword arguments and returns a partial object.
When using the functools.partial()
method, it's best to pass the arguments as
keyword arguments to not mix up the order.
You can also use the repeat
class from the itertools
module to pass multiple
arguments to the map()
function.
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))) print(new_list) # ๐๏ธ [5, 10, 15, 20, 25]
The itertools.repeat() class makes an iterator that returns the given object over and over again.
You can imagine that the map()
function calls multiply
with each item of the
list and the number 5
because that's what we passed to the
itertools.repeat()
class.
Each of the numbers in the list gets multiplied by 5
in the example.
I've also written an article on how to get access to the index in the map() function.