dictionary speed

David Bolen db3l at fitlinxx.com
Fri Jul 13 18:01:52 EDT 2001


simonb at webone.com.au writes:

> ie. a dictionary access n levels deep would incur
> time penalty proportional to n times the penalty
> due to each dictionary look up (log n i presume).

Right.  For the purpose of key search, what the dictionary actually
holds are just objects that don't get involved in the search, whether
values are themselves dictionaries won't matter.  But if you nest
dictionaries then clearly you'll have to do extra lookups at each
dictionary.  The first lookup yields the object (which happens to be
another dictionary) on which you do another lookup, and so on.

If you have a three-level nested dictionary structure that you
reference with something like:

    dictionary[key1][key2][key3]

then there are three discrete lookups occuring.  Of course, if you are
going to be using that nested dictionary a lot, you could always bind
a name to that dictionary while you use it, e.g.:

    nested = dictionary[key1][key2]

and then do single level lookups:

    nested[key3]

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list