search multiple dictionaries efficiently?

Ben Sizer kylotan at
Wed Jan 18 04:18:13 EST 2006

Duncan Booth wrote:
> Test with the 'in' operator and then retrieving the value is the fastest
> solution when the value isn't in the dictionary (it only does a single
> lookup then), and is still fast when it is. [0.36/0.2]
> Using the get method of the dictionary with a default value to be retrieved
> if the key is not present is slower than using the 'in' operator in all
> cases (it does beat try/except when an exception is thrown) [0.49/0.54]

Assuming those statistics are replicatable, it's quite unfortunate that
the obvious and concise way to do things works out more slowly than the
approach that you'd expect to take twice as long. Thankfully there
doesn't seem to be too many of these problems in Python.

Ben Sizer

More information about the Python-list mailing list