Accessing nested data

Learn this with friends

ComputingIntro to computer science - Python5:536 check-ins

About this video

Dictionary values can be lists or other dictionaries, creating nested data. The video shows how to tell a value's type from the symbol right after the colon, and how to chain accesses like gasworks["types"][0] or gasworks["reviews"][0]["author"], evaluating from the outside in, left to right.

You'll be able to

  • identify whether a dictionary value is a string, number, list or dictionary from the symbol after the colon.
  • write a chained expression to access data several layers deep.
  • update or mutate nested data using a multi-part access.
  • explain why skipping a step in a nested access causes a TypeError.

Key words

nested data
One data structure stored inside another, like a list inside a dictionary.
chained access
Several bracket accesses in one expression, evaluated left to right, like d["types"][0].
outside in
Accessing the outer structure first, then the structure inside it, and so on.
TypeError
An error from using the wrong type, like indexing a list with a string key.
Transcript

- [Instructor] What happens if we put a dictionary inside another dictionary? Dictionary values aren't limited to just primitive data types. The dictionary keys need to be immutable, but the values can be dictionaries or lists. We call this nested data because we're nesting one data structure inside of another.

But how do we access something that's nested multiple layers deep? This dictionary has location data about a place called Gasworks Park, just like normal we use the key to access the value, and we can use an assignment statement to update the value associated with the key rating.

Now, what about the item with the key types? Again, we can access its value using the key, but what data type is this value? The important thing to pay attention to here is what symbol comes right after the colon. If it's a quotation mark, that value is a string.

If it's a number, that value is an integer or float. But if that symbol is a bracket, that value is a data structure. Remember that we define a list using square brackets, and we define a dictionary using curly brackets. So if we see a square bracket, our dictionary value is a list, or if we see a curly bracket, our dictionary value is another dictionary.

Looking at this square bracket, then we know the value for the key types is a list. We can store that value in a variable, and then we can access it just like we would any other list. So if I access the element at index zero, I get the string tourist attraction because that's the first element in the list.

But here's the fun part. I can combine that two part access into a single expression. I skip the intermediate step of storing that list in its own variable and instead just tack on the second access after the first. This might look a bit intimidating, but we know how to evaluate expressions.

We just need to take it one step at a time. We follow the order of operations and we evaluate from left to right. So the first part of our expression accesses the variable gasworks, and from that it looks up the key types. That gives us our list, which we can substitute back into the expression.

And now we simplify again, we're operating on the list now and we're accessing the element at index zero. So this whole thing evaluates down to the string tourist attraction, which we store in the variable first type. Now, just because this list is nested inside a dictionary doesn't mean there's anything special about it.

We can operate on it just like we would any other list. We can use the in operator to check if an element exists in the list. That's true, or we can mutate the list and use the append method to add an element to the end. Let's move on to another item. What data type is the value for the key amenities?

Notice this curly bracket. That means we have a dictionary. So our variable features holds this inner dictionary, and we can access the values of that inter dictionary as normal. So we look up the value associated with the key parking, which gives us the Boolean true.

And just like our magic, before we can inline all those accesses into a single expression, our accesses just need to go outside in left to right. So the first part of our expression accesses the value for the key amenities, which is a dictionary. We substitute that in and then we access the value in that dictionary associated with the key parking.

And if say they close the restrooms for renovation, I can update the value in that inter dictionary using an assignment statement with that same two-part access. Now, let's take a look at all that in the code editor, and let's go one level deeper. I've added a new item to our gasworks dictionary with the key reviews.

What is going on with this value? We have square brackets here, but we also have curly brackets. Remember though, what we need to pay the most attention to is what symbol comes directly after the colon, and that's a square bracket. So that means the data type of this value is a list.

So let's access that first and print it out. Since it's a list we can index into it. So let's see, what element is it Index zero. That starts with a curly bracket, so that must mean this element is a dictionary. And then I guess we can access the values of that dictionary using the keys, like we can get the value associated with the key author, which gives us the author of that first review.

So rewind, my Gasworks dictionary has an item reviews whose value is a list of dictionaries. First, I access the list, then I access the element at index zero, which is a dictionary, and then I access the value in that dictionary associated with the key author.

If I didn't realize I was working with a list of dictionaries and say I skipped the second access, I would get a type error because I would be trying to access the list using a string dictionary key instead of an integer list index. When doing nested accesses, we just need to pay careful attention to the order.

We always go from the outside in, and if we're ever unsure, we can just take it one step at a time.

Video © Khan Academy, used under CC BY-NC-SA. Watch on Khan Academy. Puzzicle is not affiliated with Khan Academy.