Python's Not (Just) For Unicorns

An interactive introduction to programming in Python, for human beings and whoever else

Chapter 21

Why we need dictionaries

So far we know about strings, a few kinds of numbers, lists, and it’s looking pretty good. There’s one more super-important kind of data that’s left: dictionaries.

In the real world, dictionaries are things you use to look up words. Same kind of thing in Python, more or less. But before we talk about how they work, let’s talk about the problem they solve.

Say we have a friend, and we know some stuff about her.

name = "Annie"
age = 83
town = "Cleveland"

print(name, "is", age, "and is from", town)

Nothing new, nothing crazy, totally normal.

Now, say we’re the most popular person on the planet, and we have two friends, Annie and Roberto. If we didn’t know better (which we don’t right now), we might do this:

# Annie's info
name = "Annie"
age = 83
town = "Cleveland"

# Roberto's info
name2 = "Roberto"
age2 = 103
town2 = "Reno"

# Print it all out
print(name, "is", age, "and is from", town)
print(name2, "is", age2, "and is from", town2)

It’s not the best thing we’ve ever seen, let’s be honest. name seems like a fine variable name, but name2 and age2 and town2 just seem… tacky. Maybe it’s because they aren’t very descriptive?

Now imagine if we were even more popular and had even more friends! If we had dozens of friends and kept naming variables the same way, we’d be stuck with variable names like name33 and age33 and town33. It’d be terrible!

“A-ah!” you exclaim, ready to use your new programming knowledge. “What about lists? They’re great for storing lots of information!”

Yeah, that’s true, and it definitely makes sense. But let’s see what happens if we try to use lists. We have two ages, two names and two towns, so maybe we can combine each set into separate lists:

names = ["Annie", "Roberto"]
ages = [83, 103]
town = ["Cleveland", "Reno"]

# Print out Annie's info
print(names[0], "is", ages[0], "and is from", town[0])
# Print out Roberto's info
print(names[1], "is", ages[1], "and is from", town[1])

It seems a little more organized, but it’s also a lot of work to use three different lists!

It’s tough because a person is a single thing, but we also want to save multiple pieces of information about the person. What we just did separates our names and ages into different variables - we split our information into separate topics instead of separate people.

It might make sense if one single thing should be one single variable, yeah? In this case, each single person can be one single variable. In this perfect world, each person would have a variable of their own - the person has a name and an age and a town, and all of that info could live together in a single variable.

Since we tried lists once, let’s try it again. What if we could give each person a separate list so their information stays together?

annie = ["Annie", 83, "Cleveland"]
roberto = ["Roberto", 103, "Reno"]

print(annie[0], "is", annie[1], "and is from", annie[2])
print(roberto[0], "is", roberto[1], "and is from", roberto[2])

It definitely works a little better, keeping all of one person’s information in one variable. But… we’re still not happy!

The problem now is we don’t know what makes annie[0] different than annie[1]. Which one was her name and which one was her age? I forget! In the beginning when we used variables like names and ages we could be sure that one was a name and one was an age. Now, though, we have to remember what order they go in, and which number is which information. We’re way too lazy for that!

If we could combine the two approaches our lives would be a lot easier. What we seem to need is one variable for each person, with multiple named things inside. Oh hey, it turns out Python can do that!

(But you’ll need to wait for the next chapter to learn more.)

Chapter summary

We learned that lists can’t do everything, and that it makes sense to store the information about a single thing in a single variable. But we don’t know how just yet!