Y
Hacker News
new
|
ask
|
show
|
jobs
by
keedon
2912 days ago
for n in [i for i in range(100) if i%2 == 0]: print n
Will work (if a bit repetitive looking)
1 comments
BerislavLopac
2912 days ago
It's not just repetitive; this particular example actually creates a list before starting the external loop -- imagine it with range(100000000) or something. It is better if you replace [] with (), which creates a generator.
link
Herald_MJ
2912 days ago
`range()` also takes an optional `step` argument which would help here.
link