# FAQ: List Comprehension - Add With Zip

This community-built FAQ covers the “Add With Zip” exercise from the lesson “List Comprehension”.

Paths and Courses
This exercise can be found in the following Codecademy content:

## FAQs on the exercise Add With Zip

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

## Join the Discussion. Help a fellow learner on their journey.

Agree with a comment or answer? Like () to up-vote the contribution!

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I can’t figure out how to use the Zip function to add corresponding elements of two lists? The documentation does not help. Is there better documentation somewhere for beginners?

This gives the correct answer but without using the Zip function as I was asked to do:

a = [1.0, 2.0, 3.0]
b = [4.0, 5.0, 6.0]

sums = [a[0]+b[0], a[1]+b[1], a[2]+b[2]]
print(sums)

# The answer is: [5.0, 7.0, 9.0]

Same here. Could do it without zip.

a = [1.0, 2.0, 3.0]
b = [4.0, 5.0, 6.0]
sums = [a[y] + b[y] for y in range(len(a))]

Using zip i kept getting an error until i managed to do it correctly. Compare your code:

a = [1.0, 2.0, 3.0]
b = [4.0, 5.0, 6.0]

c = zip(a, b)

sums = [x + y for (x, y) in c]

I think you should rewatch the Comprehension List explanation video!

2 Likes

Really, the main problem with this “solution” is not that it fails to use zip, but that it is not in the least a generalized approach. If you had anything other than two lists containing three elements each, it would fail.

Always look twice if you find yourself tailoring an expression for a specific piece of data. In any given context, the approach could be valid, but it is probably worthwhile seeking a general solution.

1 Like

Got it, thanks. I think this is what they were looking for:

a = [1.0, 2.0, 3.0]
b = [4.0, 5.0, 6.0]
sums = [(x + y) for (x, y) in zip(a, b)]

3 Likes

Why is it not required to convert the zip(a,b) to list in this case?
I tried it with list(zip(a,b)) and without list and the output was the same.
Does the list function not return only the location to the list and not the list itself?

2 Likes

Works with and without list()

In a loop, that is correct, but we can only run that loop once since the zip object will have been consumed.

If we wish to preserve the zip object in non-consumable form then casting to a list or tuple would result in a persistent object that can be polled or iterated an indefinite number of times. The list will be mutable, but the tuple will be immutable.

1 Like

I found that the accepted answer was rigid in terms of naming variables. This is what I wrote that wasn’t “right”:

sums = [a + b for (a, b) in zip(a, b)]

But this is accepted:

sums = [item1 + item2 for (item1, item2) in zip(a, b)]

1 Like

It is not readily apparent that there are distinct objects in play given they all have the same name. `a` and `b` are list objects in `zip(a, b)` but single values in `for (a, b)`. That makes for ambiguity that should be avoided.

This code worked for me.
a = [1.0, 2.0, 3.0]

b = [4.0, 5.0, 6.0]

#sums=[i+j for (i,j) in a,b]

sums=zip(a,b)

sums=list(i+j for i,j in sums)

print(sum)