What Is a Dictionary?

So far, each variable has held one value, like a number or a piece of text, or a list of values of the same kind, like the names of your friends. In this lesson, you'll learn about dictionaries. A dictionary keeps together several values that describe one thing, and it gives each of them a name.

Imagine you want to keep a few facts about a person: their name, age and city. You could store them in separate variables:

name = "Anna"
age = 21
city = "Brno"

For one person that works, but for ten people you would need thirty variables, and nothing in the code would say which of them belong together. You could put the values in a list instead:

person = ["Anna", 21, "Brno"]

Now they are together, but you have to remember that index 0 is the name and index 2 the city. And what is person[1]? The code doesn't tell you.

A dictionary solves both problems. It keeps the values together and gives each of them a name:

person = {"name": "Anna", "age": 21, "city": "Brno"}

A dictionary is written in curly braces { }. Inside are key–value pairs, separated by commas. The key is the name of a value: "name", "age", "city". It's usually text, so it's in quotes. After the key comes a colon and then the value, which can be anything you know: text, a number, True or False, even a list.

Why “dictionary”? It works like a real dictionary: you look up a word (the key) and find what it means (the value).

A longer dictionary is easier to read with each pair on its own line. Both ways mean the same. When you print a dictionary, Python shows it on one line, with text in single quotes, as it does with lists. The pairs stay in the order in which you wrote them:

person = {
    "name": "Anna",
    "age": 21,
    "city": "Brno"
}
print(person)
{'name': 'Anna', 'age': 21, 'city': 'Brno'}

Don't join a dictionary to text with +; it doesn't work, just as it doesn't with a number. "Person: " + person ends with TypeError: can only concatenate str (not "dict") to str. Separate them with a comma instead, print("Person:", person), or use an f-string.

Typical mistakes when writing a dictionary:

  • A missing comma between two pairs: {"name": "Anna", "age": 21 "city": "Brno"} ends with SyntaxError: invalid syntax. Perhaps you forgot a comma?
  • A key without quotes: {name: "Anna"}. Python thinks name is a variable, and the program ends with NameError: name 'name' is not defined.
  • = instead of a colon: {"name" = "Anna"} ends with SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='? The message doesn't help much (you didn't mean == either), so remember it: when you see it, look for an = inside curly braces.

Instructions

Delete the three variables and the print(). Instead, create a variable pet holding a dictionary with these pairs, in this order: "name" with the value "Rex", "species" with "dog", "age" with 3 and "vaccinated" with True. Print it with print(pet). The output should be {'name': 'Rex', 'species': 'dog', 'age': 3, 'vaccinated': True}.

Below, print the dictionary once more, this time with the label Pet: in front of it. The new line should be Pet: {'name': 'Rex', 'species': 'dog', 'age': 3, 'vaccinated': True}.

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

1/9