How to pass multiple iterable as arguments to a python map? You can use python map() with multiple iterable arguments by creating a function with multiple arguments and using it on map() with multiple iterables. The map() function in Python is used to apply the transformation to an iterable object like a list, tuple, or set, etc. whereas A Python lambda function is a small anonymous function that is mainly used in combination with the functions filter(), map(), and reduce(). lambdas can also take any number of arguments.
In this article, I will explain how to use python by passing multiple arguments with examples.
1. Syntax of map() with Multiple Iterable Arguments
Below is an example of map() function that can take one or multiple iterable arguments in Python.
# Syntax of map() function
map(function, iterable, [iterable1, iterable2, ...])
2. map() Function Using Multiple Arguments
Here, I will use multiple (two) iterators as arguments to the Python map() function. First, create addtion(x,y)
that takes two arguments x
and y
and return value by adding these two. Use this function on map() function along with two iterable as arguments.
# Create function that takes two arguments
def addition(x,y):
return x + y
# Create lists
numbers1 = [2, 4, 6, 8, 5]
numbers2 = [3, 2, 1, 1, 4]
print("Numbers1: ",numbers1)
print("Numbers2: ",numbers2)
# Using map() with two iterables
addition_numbers = map(addition, numbers1, numbers2)
result = list(addition_numbers)
print("Result:",result)
# Output:
# Numbers1: [2, 4, 6, 8, 5]
# Numbers2: [3, 2, 1, 1, 4]
# Result: [5, 6, 7, 9, 9]
When the map()
function is called with addition() function and numbers1
and numbers2
as arguments, it applies the addition()
function to each element from the two arguments and returns an iterator object.
3. Use Multiple Arguments with map() & Lambda
Let’s use the same above example with the map() & lambda function by using multiple iterable arguments. When you use lambda, you don’t have to create a function addition() instead you use the definition within the lambda itself as expression.
# Use map() function & lambda with multiple arguments
numbers1 = [2, 4, 6, 8, 5]
numbers2 = [1, 3, 5, 7, 4]
addition_numbers = map(lambda x, y: x + y, numbers1, numbers2)
print(list(addition_numbers))
# Output:
# [3, 7, 11, 15, 9]
Here, the python map() function takes multiple iterable arguments as inputs and applies the lambda function to the corresponding elements of each input iterable. In this case, the first element of input iterables is passed as the first argument to the lambda function, and the second element of input iterable is passed as the second argument. The lambda function here sums up the two arguments passed to it and returns the result, so the elements of the numbers1
and numbers2
are being added.
Conclusion
In this article, you have learned to use Python map() with multiple iterable arguments. First, we have seen using multiple arguments on the map() function and later learned to use it with both lambda & map.