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 () 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 () below!
Agree with a comment or answer? Like () to up-vote the contribution!
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?
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]
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?.
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}")