I have the following two lists:
first = [1,2,3,4,5] second = [6,7,8,9,10]
Now I want to add the items from both of these lists into a new list.
output should be
third = [7,9,11,13,15]
Advertisement
Answer
The zip
function is useful here, used with a list comprehension.
[x + y for x, y in zip(first, second)]
If you have a list of lists (instead of just two lists):
lists_of_lists = [[1, 2, 3], [4, 5, 6]] [sum(x) for x in zip(*lists_of_lists)] # -> [5, 7, 9]