FAQ: Learn Python: Loops - List Comprehensions


This community-built FAQ covers the “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 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!



I solved the exercise–> [164, 170, 163, 163]. But I wanted to try to solve this also without using the list comprehension. This is my approach:

#second approach
heights = [161, 164, 156, 144, 158, 170, 163, 163, 157]
can_ride_coaster = []

for height in heights:
  if height > 161:

Output is:

[164, 170]
[164, 170, 163]
[164, 170, 163, 163]

How can I get the output of only:
[164, 170, 163, 163]

with my code? Any hints?


Prints each iteration on screen since print is inside the for loop.
It writes print out of the for loop and then of the same one being identado to the level of the other written lines, for example:


It will result:

[164, 170, 163, 163]

Greetings and successes to you!

Note: the English language is not my native language, sorry for some involuntary error of grammar.


Hi there, I have a question about the example before the exercise.
Why does the example use after an element in the list:
for word in words:
if word[0] == ‘@’:

Is it because it’s a way to check the string in the list elements?


the examples also shows the result:

["@coolguy35", "@kewldawg54", "@matchamom"]

which could help use deduce what happens.

word are elements of the list, word[0] will give us the first letter. Apparently, usernames start with a @


Hello there,
Can someone help me to walk through the logic behind the syntax for list comprehension using the example before exercise? I have hard time understanding why we need to enter temporary variable word in front of the for loop?

words = ["@coolguy35", "#nofilter", "@kewldawg54", "reply", "timestamp", "@matchamom", "follow", "#updog"]
usernames = [**word** for word in words if word[0] == '@']

Per example, it says that this list comprehension:

  1. Takes an element in words -> I I understand that this happens via the for loop:
    for word in words
  2. Assigns that element to a variable called word -> I understand that this happens with every for loop when it assigns the value of the element it has found in the list to a temporary variable that we call here word

This is where I get stuck as I do not understand why we have to write word again in code before the for loop? What program “sees” when it hits this and why it can not run the code without it?
Thank you for the logic behind it!


lets look at the example:

words = ["@coolguy35", "#nofilter", "@kewldawg54", "reply", "timestamp", "@matchamom", "follow", "#updog"]
usernames = []

for word in words:
  if word[0] == '@':

the word variable in your question about list comprehension is equivalent to .append(word)

so its what we are appending to the list, this can be quite handy, for example:

result = [x ** 2 for x in range(1, 11) if x% 2 == 0 ]

this will give us the exponents of all even numbers from 1 till 11.

so we can manipulate what we append to the list, quite useful. Hope this helps


Thank you for your prompt attention and excellent example - I get it now!

split this topic #10

3 posts were split to a new topic: Description problems in course material