loop beats generator expr creating large dict!?

Ben Cartwright bencvt at gmail.com
Tue Oct 3 07:09:08 CEST 2006


George Young wrote:
> I am puzzled that creating large dicts with an explicit iterable of
> key,value pairs seems to be slow.  I thought to save time by doing:
>
>    palettes = dict((w,set(w)) for w in words)
>
> instead of:
>
>    palettes={}
>    for w in words:
>       palettes[w]=set(w)
>
> where words is a list of 200000 english words.  But, in fact,
> timeit shows the generator expression takes 3.0 seconds
> and the "for" loop 2.1 seconds.  Am I missing something?

Creating those 200,000 (w, set(w)) intermediate tuples isn't free.  You
aren't doing that in for loop version.  If you were:

    # Slowest of all!
    palettes={}
    for w,s in ((w,set(w)) for w in words):
        palettes[w]=s

--Ben




More information about the Python-list mailing list