0
For loop in python
Hey, I've seen some for loops that have a variable name before the for statement. Can you give me an example and explain what it does?
7 Answers
+ 3
Maybe it was a list comprehension?
lst = [i for i in range(10)] # something like this?
This creates a list populated by each value that i takes in the range given.
print(lst) # [0,1,2,3,4,5,6,7,8,9]
+ 1
Ipang I cant find it.. that's why I've asked.. sorry
+ 1
Russ yes! Thank you
+ 1
Um, no. Not to get the same result. Have you tried it yourself?
0
yahel
Can you instead copy/paste that code you seen in the Description (just the loop part)? I think it makes more sense for others to explain ...
0
Russ but cant I just do it without the i before the for keyword?
0
Russ ok, thanks!