Dealing with lists of dictionaries and dictionaries with lists inside can sometimes be confusing, even if we understand how each work separately. Let’s break them all down.
A little review
Lists
First, we have lists. We can do two major things with lists:
- We can grab specific elements, and
- we can loop through each element of the list
# A list of a few numbers
numbers = [5, 32, -3]
# Get the first number and print it
first_number = numbers[0]
print(first_number)
5
# Use a for loop to print every number
for number in numbers:
print(number)
5
32
-3
Dictionaries
Then we have dictionaries. We can do a few things with lists
- We can grab a specific value using a key
- We can grab all of the keys
- We can grab all of the values (you probably don’t do this often)
# A dictionary about the film Maid in Manhattan
movie = {
'name': 'Maid in Manhattan',
'starring': 'Jennifer Lopez',
'year': 2002
}
# Print the name of the movie using the 'name' key
print(movie['name'])
Maid in Manhattan
# View all the keys of the dictionary (name, starring, year)
print(movie.keys())
dict_keys(['year', 'starring', 'name'])
Lists of dictionaries
Now that we’ve looked at the two separately, let’s combine the two.
# A list of three Heath Ledger movies
movies = [
{
'name': 'Ten Things I Hate About You',
'starring': 'Heath Ledger',
'year': 1999,
'rating': 61
},
{
'name': 'Brokeback Mountain',
'starring': 'Heath Ledger',
'year': 2005,
'rating': 87
},
{
'name': 'The Dark Knight',
'starring': 'Heath Ledger',
'year': 2008,
'rating': 94
}
]
Don’t think of movies
as a list of dictionaries - just think of it as a
list, ignore what’s inside. What can we do with a list?
- Get specific elements (the first, the last, etc)
- Loop through each element of the list
So, well, let’s do those!
# Get the first movie and print it
movie = movies[0]
print(movie)
{'rating': 61, 'year': 1999, 'starring': 'Heath Ledger', 'name': 'Ten Things I Hate About You'}
Okay, so it printed out the entire first dictionary. The same thing will happen if we loop through each element.
# Loop through each movie
for movie in movies:
print(movie)
{'rating': 61, 'year': 1999, 'starring': 'Heath Ledger', 'name': 'Ten Things I Hate About You'}
{'rating': 87, 'year': 2005, 'starring': 'Heath Ledger', 'name': 'Brokeback Mountain'}
{'rating': 94, 'year': 2008, 'starring': 'Heath Ledger', 'name': 'The Dark Knight'}
This usually isn’t helpful, since we normally only want to deal with one or two
aspects of our data (maybe the rating or the name). But think about it - each
time we pull out a specific element or each time we’re inside the loop,
movie
is a dictionary. What are our favorite things to do with a dictionary?
- We can grab a specific value using a key
- We can grab all of the keys
So let’s combine the two steps, first dealing with the list on the outside, then the dictionary on the inside.
- We have a list, so we can pull out a specific element
- That specific element is a dictionary, so we can get its keys
- Which means we can also use those keys to get data!
# MOVIES is a list, so we can get the first movie using [0]
movie = movies[0]
# MOVIE is a dictionary, so we can find the keys using .keys()
print("The movie has the keys", movie.keys())
# MOVIE is a dictionary, so we can get data using a key
print("The movie's name is", movie['name'])
The movie has the keys dict_keys(['rating', 'year', 'starring', 'name'])
The movie's name is Ten Things I Hate About You
But maybe you want to deal with all of the elements, like maybe we want to see the ratings? Let’s combine again.
- We have a list, so we can loop through all of the elements
- Each element is a dictionary, so we can get data using a key
# movies is a list, so we can loop through all of the elements
for movie in movies:
# What's inside the list? a DICTIONARY. So we can use keys to get data!
print(movie['name'], "had a rating of", movie['rating'])
Ten Things I Hate About You had a rating of 61
Brokeback Mountain had a rating of 87
The Dark Knight had a rating of 94
Dictionaries with lists inside
Another version of this is when you have a dictionary with a list inside.
# Lauren is indecisive and has many favorites
lauren = {
'age': 12,
'favorite_numbers': [4, 5, 3, 1, 4, 5],
'favorite_colors': ['blue', 'green', 'purple']
}
Since lauren
is just a dictionary, you can do dictionary things to it.
- We can grab a specific value using a key
- We can grab all of the keys
# This value is boring
print(lauren['age'])
12
# This value is more exciting!
print(lauren['favorite_colors'])
['blue', 'green', 'purple']
# You can even what data type things are using 'type'
print(type(lauren['age']))
print(type(lauren['favorite_colors']))
<class 'int'>
<class 'list'>
Now that we’ve found a list, we can do list things.
- Get specific elements (the first, the last, etc)
- Loop through each element of the list
So we ‘ll have two steps
- We have a dictionary, so specific data out using a key
- We then a list, so use
[...]
indices orfor
loops to go through each element
# STEP ONE: Use a key to get something out of the dictionary
colors = lauren['favorite_colors']
# STEP TWO: It's a list, so let's get the first one
print(colors[0])
# STEP ONE + STEP TWO all at once
print(lauren['favorite_colors'][0])
blue
blue
# STEP ONE: Use a key to get something out of the dictionary
colors = lauren['favorite_colors']
print(type(colors))
# STEP TWO: It's a list, so let's loop through them
for color in colors:
print(color)
<class 'list'>
blue
green
purple