FAQ: Learn Python: Loops - More List Comprehensions

This community-built FAQ covers the “More List Comprehensions” exercise from the lesson “Learn Python: Loops”.

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

Computer Science
Data Science

FAQs on the exercise More List Comprehensions

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!

3 posts were split to a new topic: I was having connection issues

2 posts were split to a new topic: Doesn’t 9/5 need to be in parenthesis?

I just came here to say that if you find the unfortunately named ‘list comprehensions’ extremely difficult to understand, you are not alone. The remedy, I hope, is repeated practice!

1 Like

I’ve got the right answer on this page, it’s printed out, and the interpreter is giving “expected Fahrenheit to be printed” error.

I ended up clicking on solution and it didn’t change one character of my code. Are there more bugs like this to be expected?

3 Likes

Blisteringly faster comprehension of List Comprehension using this second topic. I gave up on the exercise on the topic just prior to this one (even after reading its comments). But when I got to this topic, I understood List comprehension instantly and completed the exercise in no time. I suggest swapping the order of the two topics that teach List Comprehension.

PS. At issue in the other topic is the distracting use of ‘word for word in words if word’… I couldn’t figure out if “word” was sometimes some literal part of the syntax (and possibly only for use with strings) because it kept appearing so I started Googling around. Rest assured, though, I found other explanations but they were far too complex to be helpful. :slight_smile:

1 Like

The same thing is happening to me right now too

Hello @chloeberkowitz916130 and welcome to the Codecademy Forums!

Could you describe the issue you are having in more detail? This exercise is asking you to create a list, fahrenheit, using list comprehension, then to print it.

I think it would help if they made the exercise easier instead of making you try to figure out how to type out and structure Celsius to Fahrenheit with a general formula as hint.

I was able to get it right, but I have a question: Do list comprehensions work with more than one command/action? For example, my code is the following (the results are floats):

celsius = [0, 10, 15, 32, -5, 27, 3]
fahrenheit = [c * 9/5 + 32 for c in celsius]
print(fahrenheit)

I want to turn the floats into integers so I thought of doing this but it gives me SyntaxError (saw it from another thread):

import math
celsius = [0, 10, 15, 32, -5, 27, 3]
fahrenheit = [c * 9/5 + 32, math.floor© for c in celsius]
print(fahrenheit)

If I do math.floor() without the formula it works well too. Is there a way to do both in the list comprehension? Or is it better to just use a plain for loop or function?

Supply your expression as the argument for math.floor(), or as the argument for int()

Hello, I think there is an issue with this lesson.
Loops 11/13 List comprehension.

I see above you had a celsius to Fahrenheit conversion in the past. Now, it’s referring to grades in a physics class, but celsius is still listed. When I click “view solution” I was able to see the grades and complete the lesson.

You’re right, looks like the lesson might have been rewritten and the starter code wasn’t updated to reflect that. I just submitted a bug report.

2 Likes

Hey thanks for the report, it looks like you may have accessed this exercise in the past so you still have the old code. If you use the reset exercise button, it should give you the intended initial code.

2 Likes

grades = [90, 88, 62, 76, 74, 89, 48, 57]
We have been provided a list of grades in a physics class. Using a list comprehension, create a new list called scaled_grades that scales the class grades based on the highest score.

Since the highest score was a 90 we simply want to add 10 points to all the grades in the list.
My question is: in what sense are we scaling the grades by adding 10 to each of them? 1 compared to 90 is not the same as 11 compared to 100. Sorry for the question not being code related, but I feel there is something I am missing.

1 Like

Because we want to scale the grades based on the highest score, this means that we want the highest grade to be 100. To scale the grades up so that the high grade is 100, we want to increase every grade by the same amount. This amount should be the difference between 100 and the highest grade (weird way of scaling grades, I know). Hope this clarifies things!

1 Like

Yes, thank you for the answer!

1 Like

Hello,
I have a doubt about the video example of the bar graph.

height of bars

y_values_1=[amt - 140 for amt in rainfall_1]
y_values_2=[amt - 140 for amt in rainfall_2] #doubt, how is the height calculated? the concept didn’t get it.
for ex: if the data is:
rainfall data
rainfall_1= [153,156,147,149,151]
rainfall_2=[159,156,160,161,148]

153-140=13, what does it do? Please explain further… I understand it starts with 140, but the concept behind on how it works is where I don’t get.

Hope you help me clear this doubt! Will be grateful : )

First we would need to know where the 140 comes from. Obviously it is a constant representing some fixed offset of the actual data points.

The final data to be graphed would be,

[13, 16, 7, 9, 11]
[19, 16, 20, 21, 8]

I’m doing the List Comprehensions: Introduction lesson right now. Is this syntax a common practice?

numbers = [2, -1, 79, 33, -45]
doubled = [num * 2 for num in numbers]
print(doubled)

I find it more difficult to comprehend versus just doing:

numbers = [2, -1, 79, 33, -45]
doubled =

for number in numbers:
doubled.append(number * 2)

print(doubled)