-1

These are my lists. How do I add every number from list1 to every number in list2?

list1 = [1,2,3,4,5]
list2 = [6,7,8,9,10]
outcomelist = [7,8,9,10,11,8,9,11,12,9,10,11,12,13,10,11,12,13,14,1,12,13,14,15]
Marek Tran
  • 23
  • 5
  • 3
    Hi there, welcome to the stackoverflow community. This question has already been answered http://stackoverflow.com/questions/1720421/how-to-append-list-to-second-list-concatenate-lists – ssuperczynski Mar 24 '16 at 11:12
  • So what's your question? Can you update your question with the code that you have tried so far or add a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) to the question? – Mazdak Mar 24 '16 at 11:12
  • 4
    I don't know whether you expect `[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]`, or `55`, or `[7, 9, 11, 13, 15]`... – TigerhawkT3 Mar 24 '16 at 11:20
  • 1
    What's your expected output?... – Iron Fist Mar 24 '16 at 11:21
  • 2
    @ssuperczynski: I'm fairly sure the OP wants this instead: http://stackoverflow.com/questions/18713321/element-wise-addition-of-2-lists-in-python – FLHerne Mar 24 '16 at 11:21
  • "Every number to every number" could also mean permute all the numbers, i.e. add the sum of the first list to every element in the second list. But I concur with @FLHerne and vote to close as probably a duplicate of that question. – tripleee Mar 24 '16 at 11:30
  • The final list should look like this:[7,8,9,10,11,8,9,11,12,9,10,11,12,13,10,11,12,13,14,1,12,13,14,15] ? – Marek Tran Mar 24 '16 at 12:26
  • @TigerhawkT3 this:[7,8,9,10,11,8,9,11,12,9,10,11,12,13,10,11,12,13,14,1,12,13,14,15] – Marek Tran Mar 24 '16 at 12:42

4 Answers4

1

Use zip build-in function and list comprehension

[x + y for x, y in zip([1,2,3,4,5], [6,7,8,9,10])]

>>> [7, 9, 11, 13, 15]

Or don't do zipping if you want sum up all to all:

[x + y for x in [1,2,3,4,5] for y in [6,7,8,9,10]]

>>> [7, 8, 9, 10, 11, 8, 9, 10, 11, 12, 9, 10, 11, 12, 13, 10, 11, 12, 13, 14, 11, 12, 13, 14, 15]
Rudziankoŭ
  • 10,681
  • 20
  • 92
  • 192
  • This was helpful but if if wanted the final list look like this [7,8,9,10,11,8,9,11,12,9,10,11,12,13,10,11,12,13,14,1,12,13,14,15] ? – Marek Tran Mar 24 '16 at 12:24
1

If you want to build a new list, you can do:

list3 = [x + y for x, y in zip(list1, list2)]

If you what you want is updating list2, you canalso use enumerate to access the index and update the list:

for idx, tuple in enumerate(zip(list1, list2)):
    list2[idx] = tuple[1] + tuple[0]
zerbino
  • 21
  • 1
  • This was helpful but if if wanted the final list look like this [7,8,9,10,11,8,9,11,12,9,10,11,12,13,10,11,12,13,14,1,12,13,14,15] ? – Marek Tran Mar 24 '16 at 12:33
0

python3

add=lambda x,y:x+y
list(map(add,list1,list2))#[7, 9, 11, 13, 15]

import operator
list(map(operator.add,list1,list2))#[7, 9, 11, 13, 15]

list comperhension:

[x+y for x,y in zip(list1,list2)]#[7, 9, 11, 13, 15]
[sum([x,y]) for x,y in zip(list1,list2)]#[7, 9, 11, 13, 15]
python必须死
  • 999
  • 10
  • 12
0

Use itertools.product to make all the possible pairs:

>>> import itertools
>>> list1 = [1,2,3,4,5]
>>> list2 = [6,7,8,9,10]
>>> [x + y for (x,y) in itertools.product(list1, list2)]
>>> resultlist
[7, 8, 9, 10, 11, 8, 9, 10, 11, 12, 9, 10, 11, 12, 13, 10, 11, 12, 13, 14, 11, 12, 13, 14, 15]

You can extend this to multiple lists:

>>> list1 = [1,2,3]
>>> list2 = [4,5,6]
>>> list3 = [7,8,9]
>>> [x + y + z for (x, y, z) in itertools.product(list1, list2, list3)]

Or even variable number of lists:

>>> [sum(items) for items in itertools.products(*list_of_lists)]