Computer Science
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].
Python
Python List Manipulation
84 Likes
Answer
print("Enter two lists of same size")
L = eval(input("Enter first list(L): "))
M = eval(input("Enter second list(M): "))
N = []
for i in range(len(L)):
N.append(L[i] + M[i])
print("List N:")
print(N)
Output
Enter two lists of same size
Enter first list(L): [3, 1, 4]
Enter second list(M): [1, 5, 9]
List N:
[4, 6, 13]
Answered By
37 Likes
Related Questions
Create the following lists using a for loop:
A list containing the squares of the integers 1 through 50.
Create the following lists using a for loop:
The list ['a','bb','ccc','dddd', . . . ] that ends with 26 copies of the letter z.
Write a program rotates the elements of a list so that the element at the first index moves to the second index, the element in the second index moves to the third index, etc., and the element in the last index moves to the first index.
Write a program that reads the n to display nth term of Fibonacci series.
The Fibonacci sequence works as follows:
- element 0 has the value 0
- element 1 has the value 1
- every element after that has the value of the sum of the two preceding elements
The beginning of the sequence looks like:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …The program prompts for element and prints out the value of that element of the Fibonacci sequence.
Thus:
- input 7, produces 13
- input 9, produces 34
Hints:
A Don't try to just type out the entire list. It gets big very fast. Element 25 is 75205. Element 100 is 354224848179261915075. So keep upper limit of n to 20.