= {"cat": "meow", "dog": "woof", "horse": "neigh"}
sounds
= sounds["cat"]
cat_sound
print(cat_sound)
meow
Lists let you store lots of variables, and to access them by their location in the list. However, there are times when you want to store lots of variables, but access them using more complex relationships. One example is a dictionary
, which lets you store variables and access them using a key.
Dictionaries in Python are created using curly brackets. Make a new file called dict.py
and put this in it:
= {"cat": "meow", "dog": "woof", "horse": "neigh"}
sounds
= sounds["cat"]
cat_sound
print(cat_sound)
meow
What we did here was create a dictionary on the first line. A dictionary is created using curly brackets ({}
), in much the same way as square brackets are used for creating lists. The dictionary we created here has three items in it where each item comprises a key and a value. The value is the real data that we want to keep hold of and the key is how we can get at the data we want. The key and value are separated by a colon and each key-value pair is separated by a comma.
On the next line we access the data in the dictionary sounds
. Again, like lists we use the square brackets to ask questions of our data. In this case we’re asking the dictionary to give us the value associated with the key "cat"
and so it will return to us "meow"
.
Since dictionaries can be quite large and it can sometimes be hard to see which parts are keys and which are values, it is possible to write dictionaries over multiple lines, one line per key-value item:
= {
sounds "cat": "meow",
"dog": "woof",
"horse": "neigh"
}
= sounds["cat"]
cat_sound
print(cat_sound)
As with lists, dictionaries are dynamic so we can add entries into a dictionary.
Let’s say that we want to add in a new sound for a cow into our sounds
dictionary. The key that the data will have will be "cow"
and the value will be "moo"
. To do so we put sounds["cow"]
on the left-hand side of a variable assignment expression, as if we’re making a new variable. On the right goes the data that we want to put into the dictionary:
= {
sounds "cat": "meow",
"dog": "woof",
"horse": "neigh"
}
"cow"] = "moo"
sounds[
print(sounds)
This is saying that we want the value "moo"
associated with the key "cow"
in the dictionary sounds
.
Running it, we see:
{'cat': 'meow', 'dog': 'woof', 'horse': 'neigh', 'cow': 'moo'}
Dictionaries can be used for any key-value mapping. The example above was a mapping of an animal species (as a string) to an animal sound ( as a string). You can use any data type you wish as the value in a dictionary. For example you might make a dictionary containing the population of some cities in millions (as a float):
= {
census "London": 8.615,
"Paris": 2.244,
"Rome": 2.627,
}
or one which contains a list of authors as the key (as a string) and their books (as a list of strings):
= {
bookshelf "Terry Pratchett": ["Mort", "Jingo", "Truckers"],
"Jane Austen": ["Sense and Sensibility", "Pride and Prejudice"],
"Charles Dickens": ["Oliver Twist"],
}