w3resource

Python: Add two given lists using map and lambda

Python Lambda: Exercise-15 with Solution

Write a Python program to add two given lists using map and lambda.

Sample Solution:

Python Code :

# Create two lists 'nums1' and 'nums2' containing integers
nums1 = [1, 2, 3]
nums2 = [4, 5, 6]

# Display a message indicating that the following output will show the original lists
print("Original list:")
print(nums1)  # Print the contents of 'nums1'
print(nums2)  # Print the contents of 'nums2'

# Use the 'map()' function with a lambda function to add corresponding elements from 'nums1' and 'nums2'
# Apply the lambda function to pairs of elements from 'nums1' and 'nums2' and generate a new 'result' iterable
result = map(lambda x, y: x + y, nums1, nums2)

# Display the result after adding elements from both lists together using 'map()' and a lambda function
print("\nResult: after adding two lists")
print(list(result))  # Print the result of adding corresponding elements from 'nums1' and 'nums2'.

Sample Output:

Original list:
[1, 2, 3]
[4, 5, 6]

Result: after adding two list
[5, 7, 9]

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to find the values of length six in a given list using Lambda.
Next: Write a Python program to find the second lowest grade of any student(s) from the given names and grades of each student using lists and lambda.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/lambda/python-lambda-exercise-15.php