Write a program that takes any two lists L and M of the same size and adds their elements together to form a new list N whose elements are sums of the corresponding elements in L and M. For instance, if L=[3,1,4] and M=[1,5,9],then N should equal[4,6,13].
Answers
Explanation:
There can be many situations in which one requires to find index wise summation of two different lists. This can have a possible applications in day-day programming. Lets discuss various ways in which this task can be performed.
Method #1 : Naive Method
In this method, we simply run a loop and append to the new list the summation of the both list elements at similar index till we reach end of the smaller list. This is the basic method to achieve this task.
# Python code to demonstrate
# addition of two list
# naive method
# initializing lists
test_list1 = [1, 3, 4, 6, 8]
test_list2 = [4, 5, 6, 2, 10]
# printing original lists
print ("Original list 1 : " + str(test_list1))
print ("Original list 2 : " + str(test_list2))
# using naive method to
# add two list
res_list = []
for i in range(0, len(test_list1)):
res_list.append(test_list1[i] + test_list2[i])
# printing resultant list
print ("Resultant list is : " + str(res_list))
Output :
Original list 1 : [1, 3, 4, 6, 8]
Original list 2 : [4, 5, 6, 2, 10]
Resultant list is : [5, 8, 10, 8, 18]
Method #2 : Using List Comprehension
The shorthand for the above explained technique, list comprehensions are usually quicker to type and hence must be preferred to perform these kind of programming tasks.
# Python code to demonstrate
# addition of two list
# list comprehension
# initializing lists
test_list1 = [1, 3, 4, 6, 8]
test_list2 = [4, 5, 6, 2, 10]
# printing original lists
print ("Original list 1 : " + str(test_list1))
print ("Original list 2 : " + str(test_list2))
# using list comprehension to
# add two list
res_list = [test_list1[i] + test_list2[i] for i in range(len(test_list1))]
# printing resultant list
print ("Resultant list is : " + str(res_list))
Output :
Original list 1 : [1, 3, 4, 6, 8]
Original list 2 : [4, 5, 6, 2, 10]
Resultant list is : [5, 8, 10, 8, 18]
Method #3 : Using map() + add()
map() can also be used, as we can input the add operation to the map() along with the two list and map() can perform the addition of both the techniques. This can be extended to any mathematical operation possible.
# Python code to demonstrate
# addition of two list
# map() + add()
from operator import add
# initializing lists
test_list1 = [1, 3, 4, 6, 8]
test_list2 = [4, 5, 6, 2, 10]
# printing original lists
print ("Original list 1 : " + str(test_list1))
print ("Original list 2 : " + str(test_list2))
# using map() + add() to
# add two list
res_list = list(map(add, test_list1, test_list2))
# printing resultant list
print ("Resultant list is : " + str(res_list))
Output :
Original list 1 : [1, 3, 4, 6, 8]
Original list 2 : [4, 5, 6, 2, 10]
Resultant list is : [5, 8, 10, 8, 18]
Method #4 : Using zip() + sum()
sum() can perform the index-wise addition of the list that can be “zipped” together using the zip(). This is quite elegant way to perform this particular task.
# Python code to demonstrate
# addition of two list
# zip() + sum()
from operator import add
# initializing lists
test_list1 = [1, 3, 4, 6, 8]
test_list2 = [4, 5, 6, 2, 10]
# printing original lists
print ("Original list 1 : " + str(test_list1))
print ("Original list 2 : " + str(test_list2))
# using zip() + sum() to
# add two list
res_list = [sum(i) for i in zip(test_list1, test_list2)]
# printing resultant list
print ("Resultant list is : " + str(res_list))
Output :
Original list 1 : [1, 3, 4, 6, 8]
Original list 2 : [4, 5, 6, 2, 10]
Resultant list is : [5, 8, 10, 8, 18]
Answer:
L=eval(input("Enter first list of numbers:"))
M=eval(input("Enter second list of numbers(of same size):"))
N=[]
if len(L)==len(M):
for i in range(len(L)):
N.append(L[i]+M[i])
print("New list of sum of the corresponding elements in l and m is:",N)
else:
print("Both lists are not of same size")
Explanation: