FAQ: List Comprehension - List Comprehension Code Challenge

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

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

FAQs on the exercise List Comprehension Code Challenge

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!

For List Comprehensions Introduction, I personally find the fact that Python abstracts the data element’s update-statement to be too abstract for my technical mind. That is, this languages seems too dumbed down coming from C++ (imperative language). What I’m having a difficult time understanding is with the following code, why shouldn’t
grade + 10 be written as grade += 10 ?

grades = [90, 88, 62, 76, 74, 89, 48, 57] scaled_grades = [grade + 10 for grade in grades] print(scaled_grades)

Maybe because C++ is the only other language that I’ve learned thus far, perhaps it might explain my mental block for this concept.

I would invite you to ruminate over two parts of your post.

What exactly do you mean by that phrase AND is that really supposed to be happening in this piece of code?

I would pose a counterquestion: “why should grade + 10 be written as grade += 10” in this piece of code?

If a value (say a number) has been assigned to a variable, and we wish to update the value (i.e. change the value and assign the result to the variable), then yes we need to do so explicitly. For Example,

x = 5
print(x) # 5

x + 3 
print(x) # 5
# We added 3 but didn't assign the result to the variable, 
# so nothing useful happened.

x += 3
print(x) # 8
# Since x += 3 is shorthand for x = x + 3, so we are assigning the result to 
# the variable as evidenced by the print statement.

Now, suppose we have a list of numbers and we want to increment the numbers by 10,

grades = [90, 88, 62, 76, 74, 89, 48, 57]
for grade in grades:
    grade += 10
print(grades) # [90, 88, 62, 76, 74, 89, 48, 57]

Well that didn’t work as expected. Why? In each iteration of the loop, one of the numbers will be assigned to the loop variable grade. If we increment the grade variable, then the value of grade will indeed change. But in each iteration, a value is assigned to the loop variable. NOT a reference. So, any changes to the loop variable don’t effect the original list. If we wished to modify the original list with the updated values, then we need to work with references,

grades = [90, 88, 62, 76, 74, 89, 48, 57]
for i in range(len(grades)):
    grades[i] += 10
print(grades) # [100, 98, 72, 86, 84, 99, 58, 67]

# We directly assigned our updates by making use of the 
# index to work with references. 

In the list comprehension code posted by you, we aren’t updating the grades list. Rather, we are creating a new list scaled_grades with incremented grades. In other words, we don’t want to modify grades. We just want to work with the values in grades and then place the updated grades into a new list. We are doing this:

grades = [90, 88, 62, 76, 74, 89, 48, 57]
scaled_grades = []
for grade in grades:
    scaled_grades.append(grade + 10)
print(scaled_grades)

Note, our intent is not to modify the grades list. Instead, we just want to take the values, increment them (without changing the original values) and then place them (append) to a new list.
The above code can be written in concise form as the list comprehension:

grades = [90, 88, 62, 76, 74, 89, 48, 57]
scaled_grades = [grade + 10 for grade in grades]
print(scaled_grades)
1 Like

Thank you, this makes more sense (today) than it did several days ago. The process of absorbing and learning these languages are a combination of unique syntax with logic. However, the “logic” applied can only be fully realized until a certain level of proficiency with the syntax of such a language. So of course it wouldn’t make sense to my brain until I reached that level of experience/proficiency, etc… or as you explained it (very well done).

Regards

1 Like

Just to pipe in here with the elephant in the room answer:

grade + 10, is an expression. # value
grade += 10, is a statement. # action

There is a difference, the main one being that we cannot use statements in comprehensions, themselves expressions. Only expressions can be used in expressions.

Above, @mtrtmk gives clear examples to illustrate the difference. We only wish to add the distinction described, here: A comprehension is a value.

[expression for expression in iterable]

Values are expressions; expressions are values.

1 Like