-1

I have created two lists.

list1 = [1, 2, 3, 4, 5, 6]
list2 = [1, 2, 3, 4, 5, 6]

After creating two lists, I want an addition of the elements of list1 and list2. Each element of list1 should add to each element of list2.

I can only come-up with merging of two lists with:

list1[:] + lis2[:]

I look-up for the pythons tutorial but couldn't find anything.

How could I add elements wise to elements addition of items of two lists in python?

EDIT 1: Ok that's great, I have already come across the question to which it is marked as duplicate, but my question is different.

I want each element of list1 should add to each element of list2. For example:

list1[1] + list2[1] adds and gives 2 and 
list1[1] + list2[2] adds and gives 3 so on...

New list should have 36 elements

dawg
  • 98,345
  • 23
  • 131
  • 206
peeyush.cray
  • 15
  • 1
  • 6
  • `numpy.array(list1) + list2` is probably the simplest way :P – Joran Beasley Jul 24 '14 at 16:18
  • @Joran Beasley That's great. And, I have already came across the duplicate question. But, my question is different. I want each element of list1 should add to each element of list2. For example:-list1[1] + list2[1] adds and gives 2 and list1[1] + list2[2] adds and gives 3. New list should have 36 elements – peeyush.cray Jul 24 '14 at 16:23
  • ok I reopened it for you ... (I didnt know I could singlehandedly reopen it ...) – Joran Beasley Jul 24 '14 at 16:47
  • It is called add applied to [Cartesian Product](http://en.wikipedia.org/wiki/Cartesian_product) btw –  Jul 24 '14 at 17:58

5 Answers5

2
print [i+ j for i in list1 for j in list2]

I think ... if I understand the question right?

if you have

list1 = [1, 2, 3, 4, 5, 6]
list2 = [1, 2, 3, 4, 5, 6]

then this will result in

[ 2,3,4,5,6,7, 3,4,5,6,7,8, 4,5,6,7,8,9, 5,6,7,8,9,10, 6,7,8,9,10,11, 7,8,9,10,11,12]

which sounds like what you want

or do it the cool numpy way

from itertools import chain
l2 = numpy.array(list2)
print chain(*[l2+i for i in list1])
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
2

The process of taking two lists and producing element pairings is a 2x2 Cartesian Product

enter image description here

Once you have the 2x2 cartesian product, apply the operator to the pairings you wish; in this case sum.

Python itertools has product:

>>> import itertools as it
>>> list(it.product('xyz','123'))
[('x', '1'), ('x', '2'), ('x', '3'), ('y', '1'), ('y', '2'), ('y', '3'), ('z', '1'), ('z', '2'), ('z', '3')]

You can then use imap to apply the sum function:

>>> list(it.imap(sum, it.product(list1, list2)))
[2, 3, 4, 5, 6, 7, 3, 4, 5, 6, 7, 8, 4, 5, 6, 7, 8, 9, 5, 6, 7, 8, 9, 10, 6, 7, 8, 9, 10, 11, 7, 8, 9, 10, 11, 12]

Itertools will support an arbitrary number of lists inside of product (ie, multidimensional Cartesian Products):

>>> list(it.imap(sum, it.product([1,2,3],[3,4,5],[6,7,8],[9,10,11])))
[19, 20, 21, 20, 21, 22, 21, 22, 23, 20, 21, 22, 21, 22, 23, 22, 23, 24, 21, 22, 23, 22, 23, 24, 23, 24, 25, 20, 21, 22, 21, 22, 23, 22, 23, 24, 21, 22, 23, 22, 23, 24, 23, 24, 25, 22, 23, 24, 23, 24, 25, 24, 25, 26, 21, 22, 23, 22, 23, 24, 23, 24, 25, 22, 23, 24, 23, 24, 25, 24, 25, 26, 23, 24, 25, 24, 25, 26, 25, 26, 27]

And won't generate a bunch of throw away lists in the process.

1

You can use a list comprehension and zip (or itertools.izip for large lists):

>>> list1 = [1, 2, 3, 4, 5, 6]
>>> list2 = [1, 2, 3, 4, 5, 6]
>>> [x+y for x,y in zip(list1, list2)]
[2, 4, 6, 8, 10, 12]
>>>
1
>>> list1 = [1, 2, 3, 4, 5, 6]
>>> list2 = [1, 2, 3, 4, 5, 6]

then

>>> import operator
>>> map(operator.add, list1, list2)
[2, 4, 6, 8, 10, 12]

and

>>> [x+y for x in list1 for y in list2]
[2, 3, 4, 5, 6, 7, 3, 4, 5, 6, 7, 8, 4, 5, 6, 7, 8, 9, 5, 6, 7, 8, 9, 10, 6, 7, 8, 9, 10, 11, 7, 8, 9, 10, 11, 12]
>>> len([x+y for x in list1 for y in list2])
36
uselpa
  • 18,732
  • 2
  • 34
  • 52
1

Try this, using list comprehensions:

[e1 + e2 for e1, e2 in zip(list1, list2)]
Óscar López
  • 232,561
  • 37
  • 312
  • 386