Why is if a in dict.keys() so slow.....

Martin Franklin martin.franklin at waii.com
Thu Apr 13 06:24:53 EDT 2000


Hi,

Just thought i'd throw this out there....

This is very slow:-


if split[0] in map_dict.keys():
    print 'found it!', map_dict[split[0]]
else:
    print 'not found'

Compared to:-


try:
    print 'found it!',map_dict[split[0]] 
 
except:
    print 'Not found'
        
The dictionary had about 40000 key:value pairs and I needed to 
look through it 250000 times.....

Question is why?


Thanks,
Martin.




More information about the Python-list mailing list