List comprehensions:Conditionals

heights = [161, 164, 156, 144, 158, 170, 163, 163, 157]

can_ride_coaster = [height if height > 161 for height in heights]


The terminal is saying I have a syntax error and it is because the condition is supposed to go after the for loop but why?

Because the basic syntax for a list comprehension is:

[expression for item in list]

You have to iterate through the list of heights and then use the ‘if’ clause.


This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.