Get startedGet started for free

Using dictionaries

1. Using dictionaries

Now that you are familiar with the container sequence types, let's dive into dictionaries. People often joke that everything in Python is a dictionary, and while it is a joke, there is a tiny bit of truth to that. I find myself using dictionaries the most of any container type. Dictionaries are useful for storing key/value pair, grouping data by time or structuring hierarchical data like org charts. Let's take a look.

2. Creating and looping through dictionaries

Dictionaries hold data in key/value pairs. While the key must be alphanumeric, the value can be any other data type. It's also possible to nest dictionaries so that you can work with grouped or hierarchical data. Additionally, we can iterate over the keys and values of a dictionary. We can also iterate over the items of a dictionary, which are tuples of the key value pairs)! We create dictionaries using the dict() method or the braces shortcut. Here, I've got a list of tuples containing the name and zip for New York City Art Galleries. I'd like to turn that into a dictionary so I can quickly find the zip code without having to scan the whole list. I begin by creating an empty dictionary named art_galleries. Next, I use tuple unpacking as I loop over the galleries in the list that contains my data. Then, inside the loop, I set the name of the gallery as the key in my dictionary and the zip code as the value.

3. Printing in the loop

Finally, I'd like to find the last 5 art gallery names. By default, when using sorted on a dictionary or looping over a dictionary, we loop over the keys. So we'll print the keys which are the names. Let's talk more about accessing dictionary values by key.

4. Safely finding by key

Here I'm looking for the Louvre gallery in New York, but it's in Paris and throws an exception. Often you will know the key you want to get from the dictionary, and using the key as an index value to get the data. However, if the key is not present you will get an ugly exception that stops the execution of your code. While you could use Python exception handling, this is so common that dictionaries have a get() method just for this problem.

5. Safely finding by key (cont.)

The get method allows you to safely get a value from a key by passing the key. If the key is not found in the dictionary, it returns None. You can optionally supply a second argument which will be returned instead of None. Continuing from the prior example of looking for the Louvre, I make the same request with the get method, and supply the string Not Found to be returned if it is not present, which is exactly what happens. Finally, I supplied a valid key to the get() method and it returned the value from the dictionary. Accessing data in a safe manner is critical to ensure your programs execute properly.

6. Let's practice!

It's your turn to practice.

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.