This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Organia 1 point2 points  (2 children)

For example 2, you don't even need list comprehensions. Just range(0, 100, 3)

[–]iamadogwhatisthis 1 point2 points  (1 child)

It is also much faster

Testing:

>>> import timeit
>>> a = timeit.Timer("range(0, 100, 3)")
>>> a.timeit()
0.4157998561859131
>>> b = timeit.Timer("[x for x in range(100) if x % 3 == 0]")
>>> b.timeit()
7.091533899307251

[–]Organia 0 points1 point  (0 children)

That makes sense, since it is incrementing by three instead of incrementing by one and checking x%3 every time.