Fast Dictionary Access

Duncan Booth duncan.booth at invalid.invalid
Sat Jun 27 07:40:53 EDT 2009

Thomas Lehmann <Iris-und-Thomas-Lehmann at> wrote:

> Hi!
> In C++, programming STL you will use the insert method which always
> provides a position and a flag which indicates whether the position
> results from a new insertion or an exisiting element. Idea is to have
> one search only.
> if  data.has_key(key):
>    value = data[key]
> But this does mean (does it?) that the dictionary is searched two
> times! If so, can somebody show me how to do this in one step?

That code actually does 3 dictionary lookups and creates a temporary 
object: the first lookup is to find the 'has_key' method, then it has to 
bind the method to data which involves creating a 'built-in method' object 
and then it calls it. Only then do you get the two lookups you expected.

Replacing your code with:

   if key in data: value = data[key]

reduces the overhead to two dictionary lookups, no temporary objects or 
function calls.

The suggested alternative:

   value = data.get(key, None) 

also has two dictionary lookups: one to find the 'get' method and one to 
find the key, but as in the first case it also has the overhead of binding 
the method and then calling it.

In other words the get method is often the clearest (and therefore best) 
way to write the code, but if performance matters do a check using the 'in' 
operator (or if you know the key lookup will fail only very rarely consider 
using try..except).

In all of the above I'm assuming the code is actually inside a function 
accessing local variables otherwise accessing variables might involve yet 
more dictionary lookups.

More information about the Python-list mailing list