Use zip()
:
>>> a = [1,2,3,4,5] >>> b = [5,4,3,2,1] >>> c = [x+y for x,y in zip(a, b)] >>> c [6, 6, 6, 6, 6]
or
>>> c = [a[i] + b[i] for i in range(len(a))] >>> c [6, 6, 6, 6, 6]
c = [aa+bb for aa in a for bb in b]
looks something like this:
for aa in a: for bb in b: aa+bb
this means select 1
from a
and then skip all the elements of b
by adding them to 1
and then select 2
from a
and then skip all the values โโof b
again by adding them to 2
so you did not get the expected result.
source share