Nested list comprehension with two lists
List comprehensions are equivalent to for-loops. Therefore, [x + y for x in l2 for y in l1 ]
would become:
new_list = []
for x in l2:
for y in l1:
new_list.append(x + y)
Whereas zip
returns tuples containing one element from each list. Therefore [x + y for x,y in zip(l1,l2)]
is equivalent to:
new_list = []
assert len(l1) == len(l2)
for index in xrange(len(l1)):
new_list.append(l1[index] + l2[index])
The reason it has 9 numbers is because python treats
[x + y for x in l2 for y in l1 ]
similarly to
for x in l2:
for y in l1:
x + y
ie, it is a nested loop