I have the following two lists:
first = [1,2,3,4,5]
second = [6,7,8,9,10]
Now I want to add items of both lists into a new list.
output should be
three = [7,9,11,13,15]
I have the following two lists:
first = [1,2,3,4,5]
second = [6,7,8,9,10]
Now I want to add items of both lists into a new list.
output should be
three = [7,9,11,13,15]
The
zip
function is useful here, used with a list comprehension.If you have a list of lists (instead of just two lists):
Default behavior in numpy is add componentwise
which outputs
If you want to add also the rest of the values in the lists you can use this (this is working in Python3.5)
Assuming both lists
a
andb
have same length, you do not need zip, numpy or anything else.Python 2.x and 3.x:
This extends itself to any number of lists:
In your case,
myListOfLists
would be[first, second]
You can use this method but it will work only if both the list are of the same size: