Skip to content

Introduction to Python – Chapter 9 – Dictionaries and Sets

In this video, we’ll talk about dictionaries and sets.

For strings, lists, and tuples, we have been using integers as the indices and position numbers in order to get their values.

Dictionaries are similar to lists except that instead of using integers as the indices, dictionaries can use any string or number or immutable type as the index.

Let’s create a dictionary to demonstrate how it works.

We will create a dictionary called states.

Instead of using number indices, we will use the state’s abbreviation as the index, and the state’s full name as the value.

An index in a dictionary is called a key.

The values in a dictionary are still called values.

As a result, we call the individual elements of a dictionary, key-value pairs.

First way to create a dictionary is by creating an empty dictionary.

After creating the empty dictionary, we can assign new keys to new values.

Here I created an empty dictionary, using a pair of curly braces.

Every key points to a specific value.

We can print this dictionary.

When we run the program, we see how a dictionary is formatted.

A dictionary is enclosed by a couple of curly braces.

Each element of a dictionary has the key followed by a colon followed by the key’s value.

Each element is separated by a comma like a list.

Every key has a value.

Another way to create a dictionary is to assign a variable to a dictionary with a few key-value pairs.

For example, let’s delete this snippet of code.

Now, we’ll write the same dictionary that we deleted except we’ll pre-populate the dictionary instead of assigning separate keys to values.

When we run the program, we get the same dictionary that we printed out with our first method of creating a dictionary.

All you have to remember is a pair of curly braces, and then, the keys are followed by a colon and a value.

Before Python 3.6, key-value pairs of dictionaries were not stored in order.

I’m using Python 3.6, but you might be using a different version of Python, which means that the key-value pairs might not be in order.

From Python 3.6 and onward, a dictionary maintains insertion order by default.

To access a value in a dictionary, you use a pair of brackets like you would with a list.

If I wanted to get the full state name for California, I would write:

When I run the program, I get “California” because “California” is the value for the “CA” key.

You can delete key-value pairs like how you would delete values of a list.

Let’s try to delete the “MA”: “Massachusetts” element.

When we run the program, the “MA”: “Massachusetts” element is deleted.

We can also get the length of a dictionary too.

When we run the program, we get the length of the states, which is now 2.

Just like lists, dictionaries are passed by reference.

A variable that is assigned a dictionary variable changes the actual dictionary.

Let’s first delete the del statement.

If I set b = states, changes to b will affect states.

When I run the program, I can see that the assignment of “CA” for b changed the states dictionary.

Dictionaries are passed and assigned by reference. Not by value.

We can use for loops with dictionaries.

If you use the for loop like you would with a list, i will be the keys of the dictionary.

When we run the program, we get the keys from states.

You can get the values separately. If you want just the values, you can use the .values() function on a dictionary.

When we run this program, we’ll get the values of states.

Most of the time though, we want both the keys and the values.

To get both the keys and values of a dictionary, we use the .items() function on a dictionary.

When we run the program, we get both the key and value from states.

You can see each state abbreviation followed by the full state name.

Most of the time, I need to grab both the key and value, so I normally format my dictionary for loops using the .items() function.

We can check if a dictionary has a particular key. Like in a list, we can check for membership in a dictionary.

For example, we can check if states has the “MA” key.

When we run the program, we see the print statement, “MA is in states dictionary.”, because “MA” is a key of the states dictionary.

You might be thinking what makes a dictionary so different from lists?

Is the only difference between a dictionary and a list the fact that a dictionary can have different types like strings for the indices?

Let’s review the main differences between a dictionary and a list.

First, we’ve talked how a dictionary can have different types for its keys.

On the other hand, a list is ordered with numeric indices, starting from 0.

Second, you may have noticed already that dictionary keys are unique.

We can reassign and delete existing key-value pairs like what we did for states, but a dictionary always forbids duplicate keys.

Dictionaries are good data structures for holding unique data.

In our example, we created a dictionary to hold the abbreviations of states because we know that every state has a unique abbreviation.

In contrast, a list can hold duplicate values.

Technically, a dictionary can also have duplicate values, but since a dictionary can only have unique keys, we use dictionaries whenever we have data with unique identifiers.

Third, checking for membership in a dictionary is much faster than checking for membership in a list.

Checking for membership in a dictionary takes about a constant, fast amount of time whereas checking for membership in a list takes time proportional to the list’s length.

I emphasize performance speed in these videos.

When talking about Computer Science in Chapter 1, I said that Computer Science is about figuring out:

“What can be computed? How can we compute it? and especially How fast can we compute it?”

Paying attention to performance speeds of every data structure you use is important to being an efficient problem solver in Computer Science.

Let’s talk about sets. A set is like a dictionary except it only contains keys.

A set is just a unique collection of values.

Let’s make a set of fruits.

You will notice that a set looks very much like a list except it uses curly braces like a dictionary.

You can add new values to a set with the set.add() function.

When we run the program, you will notice that adding new values in a set does not put them in order.

Sets are not about order. If you want to keep an ordered sequence, then use a list.

You can remove values from a set, but you can only remove that value from a set if it is already in the set.

If the value you want to remove is not in the set, then you will get an error.

Let’s check for membership in the set, and if “pears” is in the set, then we’ll use the .remove() function.

When we run the program, since “pears” is in the fruits set, we remove “pears”.

We can see that fruits no longer has “pears.””

You can use a for loop with a set.

When we run the program, we will get every fruit string in the fruits set.

Let’s review the aspects of sets.

Sets cannot have duplicate values, so sets are good data structures for holding unique data.

Accessing elements of a set is a pain since sets don’t support access by index.

Checking for membership in a set is as fast as dictionaries.

The big question is when should you use lists, dictionaries, and sets?

Ask yourself:

Do you just need an ordered sequence of items? Use a list.

Do you need to associate values with keys, so you can check for membership efficiently and find the values (by key) later on? Use a dictionary.

Do you just need to know whether or not you already have a particular value but without ordering (and you don’t need to store duplicates)? Use a set.

Most of the time, you’ll be focusing on lists for ordered sequence of items and dictionaries for unique key-value pairs of data because accessing values in a set is just not very flexible.

When you know that you have to search your data for membership or you don’t want any duplicate data, think about using dictionaries or sets.


Video Demonstration