FAQ: List Comprehension - Product

This community-built FAQ covers the “Product” exercise from the lesson “List Comprehension”.

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

FAQs on the exercise Product

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 (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.

Ask or answer a question about this exercise by clicking reply (reply) below!

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

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

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!

you better work in this, super unpedagogic…answer is not even close to the exampel

2 Likes

If you would provide a link to the exercise, it would be easier to see your point!

Hi, could you please answer what if each sub-list differs in length. E.g. list = [(1, 2), (1, 2, 3)]. How do you get a product using comprehension?

1 Like

About the example with original_list & new_list and the code given in the lesson:

original_list = [[1, 2], [3, 4], [5, 6]]
new_list = [item1 + item2 for (item1, item2) in originalList]

In the first place I thought that item1, item2 refers to the each sublist itself i.e. item1 is [1,2] , item2 is [3,4].
After I copied/pasted , ran the code and looked at the solution I realised that item1 ,2 refer to the content of each sublist i.e. item1 is 1,3,5 and item2 is 2,4,6. So the result of the sum is new_list= [3,7,11].
The same obviously applies to the example with the product/nested _lists written next in the lesson.

However, I wonder how would we be able to refer to specific sublists when writing the code (or better say it, to note each sublist’s index/position in the list) .
For example, if we wanted to find the sum of only the first two sublists [1,2], [3,4] excluding the third, how would we write the code?

We could apply the code to only a slice of the original list:

 [a + b for (a, b) in originalList[:2]]
2 Likes

what is the code without list comprehension?
product = [e1 * e2 for (e1, e2) in nested_lists]

product =
for item1, item2 in nested_lists:
product[item1] = item1 * item2
print(product)

why it turned out only 966?

Should we be treating item1 as an index?

if there are three sublists why does the syntax for this exercise work that way?

Hey,
In the example, new_list = [item1 + item2 for (item1, item2) in original_list], parentheses are used to define item1 and item2.

The example could be written as new_list = [item1 + item2 for item1, item2 in original_list] and the same result occurs.

What situations would it be useful to define with parenthesis?

It’s one of those things that can go either way. Doesn’t hurt readability.

There are cases where grouping is needed for operator precedence, albeit this is not one of them.

Product

Example nothing like the answer. Where is this ‘e’ business coming from?

Not sure what “‘e’ business” you are referring to. This lesson expects a list of products, not sums.

Great question. I’d rather use a function in this case. For example, [prod(l) for l in list], where prod(l) is a function that computes a product of every elements in a list l. If list = [[1,2], [1,2,3]], then we will get [2, 6]. You can define this prod function on your own, but there already exists such a function. Here is an example:

from math import prod
lists = [[1,2], [1,2,3]]
[prod(list) for list in lists]

This will give [2,6].

I would love to hear other options. :smiley:

Hi,
What is the difference between brackets and parenthesis ? Can we use both and vice-versa ?

Is

new_list = [item1 + item2 for (item1, item2) in original_list]

equivalent to

new_list = [item1 + item2 for [item1, item2] in original_list]

?

Thanks for the feedback.

You would be creating a tuple instead of a list. They both end up in the same final result here. Since you’re not using the list again it may as well be a tuple. It’s not too important at this point but you should try to avoid creating objects you don’t need.

Technically the parentheses here aren’t necessary either, you can keep them if you think they’re more readable though-

new_list = [item1 + item2 for item1, item2 in original_list]

Ok thanks. I have the impression that in the exercice we create a tuple rather than a list then (since the results shows parenthesis). But we use list(). This I do not understand…

Print is only ever a representation of the objects stored but the standard output for a list would be using square brackets [] and parentheses for a tuple ().

print(list()) print(tuple()) print([x for x in range(0)])

Based on what you’ve written above, new_list will always be a list as it is created via a list comprehension. You’ll need to explain more or post the full code as it’s nothing but guesswork otherwise- How do I format code in my posts?.

Hi tgrtim,

Sure, I can show an example. In the exercice of Frida Kahlo, we have at the beginning the following code:

paintings = ['The Two Fridas', 'My Dress Hangs Here', 'Tree of Hope', 'Self Portrait With Monkeys'] dates = [1939, 1933, 1946, 1940] paintings = list(zip(paintings, dates)) print (paintings)

I see that the result ('The Two Fridas', 1939) is in parenthesis (even if it was defined in brackets at the beginning), while the entire nested list is in brackets.
But if I append new element of a list in this way below, the result will give me a mix of parenthesis () and brackets. Why ? What kind of object do I have created?

paintings = ['The Two Fridas', 'My Dress Hangs Here', 'Tree of Hope', 'Self Portrait With Monkeys'] dates = [1939, 1933, 1946, 1940] paintings = list(zip(paintings, dates)) print (paintings) paintings.append(['The Broken Column', 1944]) paintings.append(['The Wounded Deer', 1946]) paintings.append(['Me and My Doll', 1937]) print (paintings)

You’re correct that would indeed be a tuple (or the overall object would be several tuples stored in a list) but that’s the standard return from zip. You can see the tuple output in the following example and I’ve included some options which would create lists instead-

# what does zip return? for element in zip("a"): print(element) print(type(element)) for element in zip("a"): element = list(element) print(f"\n\n{element}") comprehension = [element for element in zip("a")] print(f"\n\n{element}")

It’s always worth having a little look at the docs for functions you may be less familiar with, the parameters and returns should be provided-
https://docs.python.org/3/library/functions.html#zip