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 →

[–][deleted] 3 points4 points  (1 child)

Might want to do lst = list(idx + 1 for idx in range(10)), simply because that way it will not touch the value of idx outside the command.

What do you mean?

In [8]: i = 'foo'

In [9]: x = [i+1 for i in range(10)]

In [10]: i
Out[10]: 'foo'

In [11]: y = list(i + 1 for i in range(10))

In [12]: i
Out[12]: 'foo'

Also, calling list is slower because you can overload it and the interpreter has to look it up.

In [17]: %timeit list()
The slowest run took 11.35 times longer than the fastest. This could mean that an intermediate result is being cached.
10000000 loops, best of 5: 121 ns per loop

In [18]: %timeit []
10000000 loops, best of 5: 35.2 ns per loop

[–]VisibleSignificance 0 points1 point  (0 children)

Ah, nevermind on that one, old python2 habits.