Is there a good reason to initiate the empty list variable first ?
It still works if I omit can_ride_coaster = []
before building the new list, and still gives a valid zero length list even if the height parameter is set so we get an empty list.
Is there a good reason to initiate the empty list variable first ?
It still works if I omit can_ride_coaster = []
before building the new list, and still gives a valid zero length list even if the height parameter is set so we get an empty list.
If using a loop with iterative assignments, then yes. If using a list comprehension, then no.
a = []
for x in range(10):
a.append(x * 10)
vs.
a = [x * 10 for x in range(10)]
Hi, can someone explain me plz, why we should/need put this temporary variable in front of our list before loop and if, because without them it dosent work an I can’t check why we need this.
below I send screenshot.
thanks
Alex
What have you read about list comprehensions? The lesson should be pretty clear about their basic construction and output. They’re called comprehensions because they are comprehensive (complete). There is the structure, the containing list, and the logic, the loop and iteration variable, and optional condition.
/ list delimiters \
[x for x in heights if x > 161]
^ \------------------------/ < logic
\
value appended to the list
All there but for an assignment or a return if it is done within a function.
can_ride = [ ... ]
Hi, thx for u answer. Look I know that it is a construction , but I don’t check , why is it so, why we should put this variable in front of. I understood that I should put my expressions in front if loop if I use comprehension, but why…
thx for your time and queenly answer
Alex
It’s by design that the expression is before the loop that defines the iteration variable from whence the expression is derived (the variable’s value is also an expression). The simplicity can be baffling but don’t let it get you too confused.
[ expression for variable in iterable ]
Eg.
arr = [1, 2, 3, 5, 8, 13, 21, 34, 55]
squares = [x ** 2 for x in arr]
------
\
expression
# [1, 4, 9, 25, 64, 169, 441, 1156, 3025]
Note also that a comprehension is itself an expression as opposed to the imperative construct of a for loop with append that it stands in for. We cannot return a for loop since it is a statement. We can return a comprehension, and we can use it anywhere an expression is appropriate.
okey, than basically it is like strong structure that I should remember and if in the future I will use this form, than so: [ expression for variable in iterable ] ?
That is the simplest form.
arr = [1, 2, 3, 5, 8, 13, 21, 34, 55]
squares = []
for x in arr:
squares.append(x ** 2)
Both this and the comprehension above produce the same result. We cannot get away from using a defined data structure for the result.
Consider we have some code that runs once through and has no need of the data once it’s run. The data structure is still in memory.
Comprehensions are expressions so may be written in place, their data consumed and then poof, gone when we’re finished. Nothing is left in memory. Granted, this is vague, and not something to labor over just yet. It will reveal itself, in time. For now, concentrate on writing a comprehension from a loop, and vice versa. Practice is so important, else the intricate details and various use cases might never become apparent.
Okey, thx for u help!
I never got my head around list comprehensions. until this comment:
Thank you for that!
So the below code is functional to accomplish the objective of the exercise:
heights = [161, 164, 156, 144, 158, 170, 163, 163, 157]
can_ride_coaster = [height for height in heights if height > 161 ]
print(can_ride_coaster)
But I do not understand why the below:
heights = [161, 164, 156, 144, 158, 170, 163, 163, 157]
can_ride_coaster = [print(height) for height in heights if height > 161 ]
print(can_ride_coaster)
Outputs this:
164
170
163
163
[None, None, None, None]
Is not that he value of “height” changes, I am just printing in the second example for checking purposes, but why in the end the list None instead of the printed value for each iteration that fell in the condition?
This post will clear your doubt → Why does my print statement print “none”? - Frequently Asked Questions / Python FAQ - Codecademy Forums