How to Pass Multiple Arguments to the map() Function in Python?

Estimated read time 2 min read

To pass multiple arguments to the map() function in Python, you can use the map() function with multiple iterables. Here’s an example code snippet that shows how to pass multiple arguments to the map() function:

# Define two lists
list1 = [1, 2, 3]
list2 = [4, 5, 6]

# Define a function that takes two arguments
def my_function(x, y):
    return x + y

# Use the map() function to apply the function to the two lists
result = map(my_function, list1, list2)

# Print the result
print(list(result))

In this example, the code defines two lists list1 and list2 that contain integer values. It then defines a function my_function that takes two arguments x and y and returns their sum. The map() function is used to apply my_function to the two lists list1 and list2. The resulting map object is then converted to a list using the list() function, and the result is printed using the print() function.

When multiple iterables are passed to the map() function, the function specified as the first argument is applied to the corresponding elements of each iterable. In this example, the my_function function is applied to the first elements of list1 and list2, then to the second elements, and so on. The resulting values are then returned as a map object, which can be converted to a list using the list() function.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply