Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to pass multiple arguments to a map function in Python?
The map() function in Python applies a given function to each element of one or more iterables and returns an iterator. When working with multiple iterables, map() applies the function element-wise across all provided arguments.
Syntax
map(function, iterable1, iterable2, ...)
The function must accept the same number of arguments as there are iterables provided.
Using Multiple Arguments with Regular Function
Example
Let's create a program to add corresponding elements from two lists ?
def add(a, b):
return a + b
# Create two lists
list1 = [1, 3, 5, 7]
list2 = [2, 4, 6, 8]
# Using map() function with two arguments
result = list(map(add, list1, list2))
print("1st list =", list1)
print("2nd list =", list2)
print("After addition:", result)
1st list = [1, 3, 5, 7] 2nd list = [2, 4, 6, 8] After addition: [3, 7, 11, 15]
The add() function takes two parameters and returns their sum. The map() function applies this function to corresponding elements from both lists.
Using Multiple Arguments with Lambda Function
Subtraction Example
Here's how to subtract corresponding elements using lambda ?
# Create two lists
list1 = [10, 15, 20, 25]
list2 = [2, 4, 6, 8]
# Using map() with lambda function
result = list(map(lambda a, b: a - b, list1, list2))
print("1st list =", list1)
print("2nd list =", list2)
print("After subtraction:", result)
1st list = [10, 15, 20, 25] 2nd list = [2, 4, 6, 8] After subtraction: [8, 11, 14, 17]
Multiplication Example
Let's multiply corresponding elements from two lists ?
# Create two lists
list1 = [3, 8, 2, 5]
list2 = [2, 4, 6, 8]
# Using map() with lambda for multiplication
result = list(map(lambda a, b: a * b, list1, list2))
print("1st list =", list1)
print("2nd list =", list2)
print("After multiplication:", result)
1st list = [3, 8, 2, 5] 2nd list = [2, 4, 6, 8] After multiplication: [6, 32, 12, 40]
Working with Three Arguments
Example
We can also use map() with three or more iterables. Let's combine three lists into tuples ?
names = ["Alice", "Bob", "Charlie"]
ids = ["ID001", "ID002", "ID003"]
scores = [85, 92, 78]
# Combine three lists into tuples
result = list(map(lambda name, id_num, score: (name, id_num, score), names, ids, scores))
print("Combined data:")
for item in result:
print(item)
Combined data:
('Alice', 'ID001', 85)
('Bob', 'ID002', 92)
('Charlie', 'ID003', 78)
Key Points
| Aspect | Description |
|---|---|
| Function Arguments | Must match the number of iterables |
| Iteration Length | Stops when shortest iterable is exhausted |
| Return Type | Iterator (use list() to convert) |
| Lambda vs Regular | Lambda for simple operations, regular functions for complex logic |
Conclusion
Python's map() function with multiple arguments provides an elegant way to perform element-wise operations across multiple iterables. Use lambda functions for simple operations and regular functions for more complex logic.
