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!
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.
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):
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?
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.
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.
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.
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!
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 : )