The top code is mine, the bottom code is the given solution.
(My code)
can_ride_coaster = [ height > 161 for height in heights]
print(can_ride_coaster)
(Solution Given)
can_ride_coaster = [height for height in heights if height > 161]
print(can_ride_coaster)
Why does my code print out True False? What would make it print out the true integers?
Also to me "height for height in heights " seems clunky. Why is that the correct phrasing ?
height > 161
is a conditional that’s True
or False
so
[ height > 161 for height in heights]
will create a list that filled with elements that are True
or False
Each element in the list heights is temporarily stored as height (one at a time), and the result of comparing this value to 161 is stored in a new list.
In the solution,
the if height > 161
limits the values that are stored as height
to only the elements in the heights
list greater than 161.
height for height
just means to use exactly what height
is for the new list without any changes.
It may be easier to read if a different word was used
[value for value in heights if value > 161]
but it still doesn’t sound like English.
Thanks for response. That helped