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

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to