Page 1 of 2
Python is quite well provided with data structures but arguably the most sophisticated is the dictionary. In this article we take a beginner's look at what makes it special.
The Python dictionary is also known as an associative array or hash table. None of the names are particularly accurate or informative and they tend to color the way people think of the data structure. For example, in this case the dictionary isn't anything to do with putting things into alphabetic order. So let's try and forget what we call it and concentrate on what it does.
A dictionary is a "magic" machine that you can use to store things in such away that you can always find them again and find them very quickly.
For example suppose you want to keep track of how old people are. You can do this using a dictionary with almost no additional code.
For example to store a person's age you would use:
The first instruction sets age to be an empty dictionary. After you have created an empty dictionary you can then store something in it, which is what the second line does.
Notice that you have to specify the person's name or some other identifier as part of the assignment. To get Lucy's age back all we have to do is:
The dictionary is a machine that when you give it two pieces of data - a key and a value - it stores the value in association with the key. Then, if later you give it the key, it gives you the value you stored earlier.
The value can be any type of data that you care to work with, and this is one of the powerful features of a dictionary.
So to summarize:
- A dictionary stores a value in association with a key
- You store the value using:
That's all there is to a dictionary in principle but in practice there are some minor complications. There is also a lot of similarity between a dictionary and a list - enough to occasionally confuse the beginner.
For example, if you wanted to store Lucy's age in a list you would write:
and to retrieve the age:
Notice that in this case the integer zero plays the role of the name "Lucy".
In both cases we are using an index to access the data in a data structure. In the case of a list you can only use integer indexes, but for a dictionary you can use a much wider range of types - although in practice you mostly use strings or integers.
There are some other important differences between lists and dictionaries. In particular you can't use an element of a list unless it has already been created in one way or another, but for a dictionary it is permissible. That is, if age_list is a list then
will result in a runtime error unless there is already an element zero.
never causes an error and will either create the new entry or update an existing one.
To make the list assignment work we have to initialize the array so that it already has a zero element. For example, you can create a list with four elements all zero:
works, but of course you have to avoid trying to assign to other elements that don't exist such as age_list,
This is a real distinction between lists and dictionaries:
- a list assignment updates existing elements
- a dictionary assignment creates and or updates elements
To initialize a dictionary you use curly brackets to distinguish it from a list which uses square brackets.
is a dictionary
is a list
Just as you can initialize a list by writing its elements within square brackets, e.g.
you can initialize a dictionary using curly brackets:
Notice that the initialization is a tiny bit more complicated because you have to specify the key and the value. The way that you do this is to use the notation key:value, but apart from this the two usages are similar. Also notice that we do need quotes around each of the keys because in this case they are strings.
The need to specify a key when you store something in a dictionary also means that there is no equivalent of any of the "positional" methods that you learned to use as part of lists. For example a dictionary doesn't support append and it doesn't support a simple pop method. Nearly all operations with a dictionary involve using a key.
You can, however, use len(dict) which returns the number of items stored in the dictionary, dict. Also del(key) will delete the entry, i.e. the key and the value corresponding to key - but you will get an error if the key isn't present.
Although I said that you can't use pop there is a pop(key) method which returns the value associated with the key and then deletes the key-value pair. There is also the slightly bizarre but occasionally useful popitem which returns an arbitrary key value pair and then deletes the entry.
There are many other dictionary methods some of which are explained a little later.