Re:Python Multiple Assignment Is a Puzzle
by ghshephard on Sunday July 27, 2014 @06:15AM
(#8092397)
None that I know of - As a novice python programmer, I just try and use comprehensions for everything so my eyes get used to the pattern.
The set function is somewhat faster.
a=[]
for x in range(10000):
a.append(randint(1,2000))
%timeit b=set(a)
1000 loops, best of 3: 373 µs per loop
%timeit b={x for x in a}
1000 loops, best of 3: 542 µs per loopstyle
Re:Python Multiple Assignment Is a Puzzle
by michaelmior on Sunday July 27, 2014 @07:55AM
(#8092522)
While comprehensions are definitely standard Python, I would argue that if you're not actually changing any value (as is the case here, then just `set(a)`, being much simpler, is more Pythonic.
Is there an advantage in using a set comprehension over set(a) or is it just a stylistic choice?