Fast Dictionary Access

Rachel P msrachel.e at gmail.com
Sat Jun 27 12:41:13 EDT 2009


[Thomas Lehmann]
> 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.
>
> <code>
> if  data.has_key(key):
>    value = data[key]
> </code>
>
> 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?

Several thoughts for you:

* Python isn't C++

* Dict lookups in Python are ubiquitous

* Trying to avoid them is often an exercise in futility

* Because of cache effects, double lookups are *very* cheap

* If this particular fragment is critical, consider using get():

    data_get = data.get
    ...
    # inner-loop code
        value = data_get(key)     # assigns None for missing values

* Another alternative is a try-block:

    try:                   # setup is cheap
         value = data[key]
    except KeyError:       # matching is expensive
         ...

* Or you can use collections.defaultdict() or a dict subclass that
defines __missing__().

* In general though, think of dicts as one of Python's most optimized
structures, one that should be embraced rather than avoided.

* Tim Peter's take on the subject from year's ago (paraphrased):
"Anything written using Python dictionaries is a gazillion times
faster than C".



Raymond



More information about the Python-list mailing list