Pass multiple arguments to the map() function in Python

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
3 min

banner

# Table of Contents

  1. Pass multiple arguments to the map() function
  2. Using a list comprehension instead of the map() function
  3. Pass multiple arguments to the map() function using partial()
  4. Pass multiple arguments to the map() function using repeat()

# Pass multiple arguments to the map() function

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.

main.py
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]

pass multiple arguments to map function

The code for this article is available on GitHub

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.

main.py
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.

# Using a list comprehension instead of the map() function

This is a three-step process:

  1. Use a list comprehension to iterate over each element of the iterable.
  2. Call the function, passing it each element and the extra arguments.
  3. The list comprehension will call the function for every element.
main.py
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]

using list comprehension instead of map function

The code for this article is available on GitHub

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.

# Pass multiple arguments to the map() function using partial()

This is a three-step process:

  1. Pass the handler function and the arguments to the functools.partial() method.
  2. Pass the result and the iterable to the map() function.
  3. The handler function will get called the arguments on each iteration.
main.py
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]

pass multiple arguments to map function using partial

The code for this article is available on GitHub

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 the partial object is called, it behaves as if the function was called with the provided arguments.

When using the functools.partial() method, it's best to pass the arguments as keyword arguments to not mix up the order.

# Pass multiple arguments to the map() function using repeat()

You can also use the repeat class from the itertools module to pass multiple arguments to the map() function.

main.py
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 code for this article is available on GitHub

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.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev