
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Pass Multiple Arguments to Map Function in Python
The map function is an in?built function in Python, which applies a given function to each and every element in an iterable (list, tuple, etc.) and returns an iterator of the result in the list. We can also use multiple arguments in the map function.
Syntax
This syntax is used in all the following examples ?
map(function, argument1, argument2,.....)
Method 1: Use multiple arguments with map function
Example
write a program to add two list elements with the help of map function.
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 sum = map(add, list1, list2) result = list(sum) print("1st list = ",list1) print("2nd list = ",list2) print("After addition of both list elements Result is :",result)
Output
1st list = [1, 3, 5, 7] 2nd list = [2, 4, 6, 8] After addition of both list elements Result is : [3, 7, 11, 15]
In this code, a function called add() is defined which returns the result of adding two numbers. Then, two lists list1 and list2 are created. map() function with two arguments, passing the add function with the first argument and list1 and list2 with the second argument. The map() function joins the values ??of two lists to form a new list. This new list is stored in the result . Finally, the list1, list2, and the result after adding the values ??of the two lists to the result are printed.
Method 2: Use multiple arguments with map & Lambda function
Example 1
write a program to Subtract two list elements with the help of map and lambda function.
# Create two lists list1 = [3, 7, 11, 15] list2 = [2, 4, 6, 8] # Using map() & lambda function with two arguments sub = map(lambda a, b: a - b, list1, list2) result = list(sub) print("1st list = ",list1) print("2nd list = ",list2) print("After subtraction from 1st list to 2nd list elements result is :",result)
Output
1st list = [3, 7, 11, 15] 2nd list = [2, 4, 6, 8] After subtraction from 1st list to 2nd list elements result is : [1, 3, 5, 7]
In this code, two lists named list1 and list2 are created. Using map() and the lambda function, the values ??in list1 are subtracted from the values ??in list2 to form a new list. For this, two arguments are passed to the map() function along with the lambda function, where list1 and list2 are formed by the lambda function with the first and second arguments. The map() function is useful for creating a new list. This new list is stored in the result . Finally, the result is printed by writing the list1, list , and the difference between the values ??of the two lists.
Example 2
In this program we are finding the product of two lists.
# Create two lists list1 = [3, 8, 2, 5] list2 = [2, 4, 6, 8] # Using map() & lambda function with two arguments multi = map(lambda a, b: a * b, list1, list2) result = list(multi) print("1st list = ",list1) print("2nd list = ",list2) print("After multiplication result is :",result)
Output
1st list = [3, 8, 2, 5] 2nd list = [2, 4, 6, 8] After multiplication result is : [6, 32, 12, 40]
In this code, two lists named list1 and list2 are created. Using map() and the lambda function, the values ??in list1 are multiplied by the values ??in list2 to form a new list. For this, two arguments are passed to the map() function along with the lambda function, where list1 and list2 are formed by the lambda function with the first and second arguments. The map() function is useful for creating a new list. This new list is stored in the result . Finally, list1, list2, and the result of multiplying the values ??of the two lists results are printed.
Example 3
In this program we are combining three lists into tuples.
name = ["Ajay","Rohit","Vikas"] id = ["DE12","DE13","DE14"] marks = ["83%","92%","99%"] res = map(lambda a, b, c: (a, b, c), name, id, marks) result=list(res) print(result)
Output
[('Ajay', 'DE12', '83%'), ('Rohit', 'DE13', '92%'), ('Vikas', 'DE14', '99%')]
In this code, three lists named name, id, and marks are created. Using map() and the lambda function, the new list is created by concatenating the values ??of name, id, and marks as triplets. For this, three arguments are passed to the map() function along with the lambda function, where name, id, and marks are constituted by the first, second, and third arguments by the lambda function. The map() function is useful for creating a new list. This new list is stored in the result . Finally, a new list result is printed, which contains triplets of the values ??for name, id, and marks.
Conclusion
In conclusion, Python's map() function makes it possible to manipulate iterable objects, especially lists. This creates code that is easy to understand and simple while executing complex operations on data.