Python's Not (Just) For Unicorns

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

Chapter 23

Getting fancy with dictionaries

Last chapter we learned about dictionaries, which are good for two things:

First off, storing multiple pieces of information about a single thing. For example, the age, name and birthplace of a person.

annie = {
  'name': 'Annie',
  'age': 83,
  'town': 'Cleveland'
}

Secondly, storing a single piece of information about multiple things. For example, the population of many different cities.

populations = {
  'Santiago': 7.2,
  'San Diego': 1.407,
  'Nara': 1.401,
  'Moscow': 12.19
}

Adding new values to dictionaries

So far we’ve only worked with totally complete dictionaries. It’s easy to add new values, though! Below you’ll see we have population data for Santiago, San Diego, Nara, and Moscow, then we add the data for New York City.

populations = {
  'Santiago': 7.2,
  'San Diego': 1.407,
  'Nara': 1.401,
  'Moscow': 12.19
}
populations['New York City'] = 8.623
print(populations)

It’s a lot like a normal variable, where you use an equals sign. The only difference is that instead of a normal variable like city you’ll use the name of the dictionary and the key, like populations['New York City'].

Now it’s your turn! Try to add scuba diving as a sport that Annie enjoys. Use the same technique we used above, giving the key and the =.

annie = {
  'name': 'Annie',
  'age': 83,
  'town': 'Cleveland'
}
  • Hint: The key is sport and the value is scuba diving
  • Hint: Don’t add it as 'sport': 'scuba diving'! We want to add it as a new line.
  • Hint: annie['sport'] is how we’ll access the part of the dictionary we’re interested in. How do we add a value to it?
  • Hint: Since scuba diving is a string, remember to put quotes around it
  • Hint: Try out annie['sport'] = 'scuba diving'

Doing some tricks with dictionaries

populations = {
  'Santiago': 7.2,
  'San Diego': 1.407,
  'Nara': 1.401,
  'Moscow': 12.19
}

Now let’s learn how to talk about dictionaries a little bit better. The things on the left - the words you can use to look up information - are called keys. 'Santiago', 'San Diego', 'Nara' and 'Moscow' are all keys in the example above. The information that you get back are called the values. 7.2, 1.407, etc are all **values.

If we compare a Python dictionary to a real dictionary, the key is the word and the value is the definition. I snuck this one by you last chapter by using the proper terms when I asked you to make a dictionary about Earl Scruggs:

key value
name Earl Scruggs
instrument banjo
birth 1924
city Flint Hill, North Carolina
genre bluegrass

Sometimes when you have a dictionary, it isn’t one that you made yourself! Maybe it’s from a file, or from somewhere on the internet, and you have no idea what’s hiding inside. Now that we know the terms key and value, we can ask these sorts of questions.

If I the annie dictionary about our buddy Annie, I can ask it what kinds of things it knows about her by typing annie.keys().

annie = {
  'name': 'Annie',
  'age': 83,
  'town': 'Cleveland'
}

print(annie.keys())

Look at that! All of the keys, laid out for us in a nice little list. Name, age and town. Very nice.

Now I’m going to throw you into the deep end of the pool without actually giving you the answer: if we have the population dictionary, how can I get a list of the values? Make your best guess!

populations = {
  'Santiago': 7.2,
  'San Diego': 1.407,
  'Nara': 1.401,
  'Moscow': 12.19
}
  • Hint: You’ll need to print this out using the print() function
  • Hint: If you’re cutting and pasting, be sure you’re using populations, not annie!
  • Hint: If .keys() gave us the keys, what do you think gives us the values?
  • Hint: Try printing out populations.values()

All of the values, right there, as a list! So nice, so beautiful.

Now that we’ve got that somewhat covered, I’m going to show you one of my favorite sneaky tricks. Last chapter I gave you a dictionary of the boroughs of New York, and told you to add all of them up. You probably did pop['Brooklyn'] + pop['Staten Island'] + pop['Manhattan']....etc, which took a lot of time to type. Is there a faster way to do it?

pop = {
  'Brooklyn': 2.637,
  'Staten Island': 0.474,
  'Manhattan': 1.645,
  'Queens': 2.339,
  'Bronx': 1.455
}
  • Hint: Don’t use pop['Brooklyn']! We’re looking for shortcuts.
  • Hint: How do you print out a list of all of the population numbers?
  • Hint: Remember, the population numbers are the values
  • Hint: Try printing out pop.values() to get a list of all of the population numbers
  • Hint: Since pop.values() gives you a list, do you know a way to add up all of the numbers in a list?
  • Hint: or a way to… sum up the values in a list?
  • Hint: sum(...) will add up the elements inside of a list for you
  • Hint: Keep track of all of those parentheses!
  • Hint: Try sum(pop.values()) to add up all of the dictionary values

Chapter summary

We learned about using .keys() and .values() to get specific information out of dictionaries.