[Tutor] Careful Dictionary Building

Chris Fuller cfuller084 at thinkingplanet.net
Fri Dec 28 18:05:00 CET 2007


first thing.. you are duplicating a lot of effort inside your loops. getting 
rid of that will speed things up:

dict = {}
for record in list:
    rid = record[0]
    if rid in dict:
        dict[ rid ].append( record )
    else:
        dict[ rid ] = [record]

The other thing I see isn't a speed problem. You are overwriting two python 
built-in types when you use variables named "list" and "dict". You probably 
want to avoid doing this, because someday yuou want to use the built-ins, and 
you code wil exhibit goofy errors when you try.

The only thing I can suggest that might speed it up more is to use a set type 
instead of a list, if the ordering doesn't matter to you. I've seen that 
change result in 20-30 percent speedups, but YMMV. If you can't start with a 
set, you can create a new one by passing the list to the set constructor. 
This may or may not help you speedwise, but try it out.

You could play around with psyco, but I don't know if it would help much in 
this case.

Chris


More information about the Tutor mailing list