[poll] anyone else needs fast random element access off a dictionary?

Dan Schmidt dfan at dfan.org
Tue Feb 11 12:43:21 EST 2003


Michal Vitecek <fuf at mageo.cz> writes:

| i'd like to ask if there exist people (like me) who have a need to
| get random elements off a dictionary and cannot use lists because
| they need a fast data lookup/retrieval.

I wrote an article about this for The Perl Journal a couple years ago,
which you can find in O'Reilly's recent Best Of The Perl Journal
collection.

If you maintain a separate list of the keys in the dictionary, and
keep the list and the dictionary in sync, you can perform all your
operations in the same order time as before, while having O(1)
random-select.

It's not as fast as if it were implemented internally in C, but it
might be sufficient for your needs.

Dan

-- 
http://www.dfan.org




More information about the Python-list mailing list