[Tutor] List comprehension for dicts?

Steven D'Aprano steve at pearwood.info
Fri Aug 20 03:00:10 CEST 2010


On Fri, 20 Aug 2010 09:51:08 am Pete wrote:

[...]
> Ah, so list comprehensions are a purely syntactic construct?

No, I don't think that's what Emile was trying to say. It's not like 
list comps are macros that are expanded into the explicit for-loop like 
that. All he is saying is that both the for-loop and the list 
comprehension loop over the list.

There are some functional differences: for example, the list comp avoids 
needing to look up result.append over and over again. Because it knows 
it is dealing with a list, rather than some arbitrary object that 
conceivably might have some weird append method with strange 
side-effects, it can take optimizing short-cuts that aren't applicable 
to general for-loops. 


> I had the feeling there might be a performance benefit of some kind.

If you write the for-loop in the most straight forward, obvious fashion, 
then there is a small but measurable performance cost from repeatedly 
looking up the append method. On the other hand, for-loops are more 
general -- you can call break to exit early, or continue to skip an 
iteration, so they're easier to optimize. There's no way to break out 
of a list comp early (other than raising an exception, which defeats 
the purpose). No matter how fast you can perform a loop, it's always 
faster to avoid it altogether, so this:

seq = xrange(10000000)
result = []
for i in seq:
    if i >= 10: break
    result.append(i%2)


will be much faster than this:

seq = xrange(10000000)
result = [i%2 for i in seq if i < 10]


Other than that, the speed of the loop itself is virtually the same as 
the speed of the list comprehension.



-- 
Steven D'Aprano


More information about the Tutor mailing list