[Tutor] Why dictionaries?

Alan Gauld alan.gauld at btinternet.com
Fri Jan 23 15:07:36 CET 2009

"Vicent" <vginer at gmail.com> wrote

> When is it / isn't it useful to use dictionaries, in a Python 
> program?
> I mean, what kind of tasks are they interesting for?

They are interesting for any place where you need to store
associations of objects and data. Think of a glossary or an index
in a book. Both are a type of dictionary.
A Glossary takes a single word and returns a paragraph of definition.
An index takes a word and retirns a list of page numbers where
that word appears.

A dictionary can also model a simple database where a single
key can retrieve an entire record. It goes on and on, they are
one of the most powerful data structures available to us.

The biggest snags are that they are not ordered so if you need
a sorted data store dictionaries may not be  the best choice.

> Maybe you can give me some references where they explain it.

Try Wikipedia under Associative Array (a fancy term for a dictionary)
or Hash Table for a description of the inner workings...


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list