Check out a free preview of the full Python Fundamentals course

The "Dictionaries" Lesson is part of the full, Python Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Nina introduces how dictionaries are composed in Python, then goes on to demonstrate how to access values, and set default values if the value specified is null.


Transcript from the "Dictionaries" Lesson

>> Nina Zakharenko: Dictionaries and Python. They're a useful type that allow us to store our data in key value pairs. So someone brought up JSON, JSON is key value pairs, right? So, perfect data structure for storing and working with JSON. Dictionaries themselves are mutable, you can add and remove items from a dictionary.

But dictionary keys can only be immutable types right. What happened when we try to put immutable lists in our set?
>> Nina Zakharenko: We got an error right? Unhashable type, same thing with dictionaries, a dictionary key can only be of immutable type. Because under the hood it uses that hash function.

>> Nina Zakharenko: We use dictionaries when we wanna be able to quickly access additional data associated with a particular key.
>> Nina Zakharenko: Practical application for dictionaries is JSON or something like memoization. Let's say that you wanna save computing power, and store the result for a function called with a particular set of immutable arguments.

The arguments themselves could be a key, and the result could be stored as the value of a dictionary, right? Just like with sets, checking to see if a key in a dictionary is a very fast operation. Like with list you don't have to say let me check each key one by one, it's just, the key is in there or not, it's very fast.

>> Nina Zakharenko: We have our dictionary cheat sheet right here, but let's kinda see them in practice
>> Nina Zakharenko: How do we make an empty dictionary? We already know this one.
>> Class: Records.
>> Nina Zakharenko: Right.
>> Nina Zakharenko: And to confirm, check the type, it's a dict. We can also, see, if the class is dict, we can also call dict like this, also have a dict.

>> Nina Zakharenko: Kind of for all of these data structures. Generally you'll see them created with the symbols and not with the methods. But, it's kind of an option in you will see it used in code. Especially with empty set where there's just is no other options.
>> Nina Zakharenko: Okay.
>> Nina Zakharenko: To create dictionaries with items in them so, the brackets, those kind of belong to sets.

The thing that makes the dictionary different than a set is a new symbol. And so, we're gonna make a dictionary, the key is on the left hand side followed by a colon, followed by the value.
>> Nina Zakharenko: So that's a one item dictionary.
>> Nina Zakharenko: Let's make a new one.

This time let's reverse the order and say the string one, the key one is mapped to the value numeric 1. Let's do that with 2 and with 3.
>> Nina Zakharenko: Notice that the key value pairs in the dictionary are separated by commas.
>> Nina Zakharenko: If we look at the type of our nums it's a dictionary, and if we look in here we'll see our key value pairs.

Just a quick sidenote about what can be used as keys. Here you've seen me use strings and numbers, right? So, I can say a dictionary of 1 to 1, that's just fine. I can have a mutable type as the value in my dictionary just fine.
>> Nina Zakharenko: What if I tried to use the mutable type as the key to my dictionary, what's gonna happen.

>> Class: Type error.
>> Nina Zakharenko: Unhashable type list, right? Only immutable types can be used as dictionary keys.
>> Nina Zakharenko: But it doesn't matter for the values, the values can be mutable, inmutable, whatever you want.
>> Nina Zakharenko: Okay so, just like with a set, the keys of our dictionary are not ordered, they're not at position 1 or 0 or anything like that.

Instead, our dictionary contains key value pairs. So looking back at our nums dictionary, how do you think I might access the value for the key 1?
>> Class: With the key.
>> Nina Zakharenko: With the key, exactly right, and so just like with a list where I used this square bracket notation to say, give me the item at this position.

With the dictionary instead you put the key in there.
>> Nina Zakharenko: You put in the key and get back the value.
>> Nina Zakharenko: Okay, what if I try to access, let's say,
>> Nina Zakharenko: What if I try to access nums at position 0, what am I gonna get? Do dictionaries have a sequence or an order to them?

No, you get a KeyError. Says 0 is not in my dictionary. If it was, then I would get something back, but that's kind of coincidence, right?
>> Nina Zakharenko: So, it's something to be mindful of.
>> Nina Zakharenko: If we try to access an item that's not in the dictionary, we're gonna get a key error, just like we saw here.

But remember with our set, there was a discard method that allowed us to remove something from the site if it was in there. And if it wasn't, instead of giving us this noisy error, it would just kinda return nothing. Dictionaries have these too, so looking at my nums.

If I called nums.get with a key that's not present no error. What do I see if I try to access the key for a nums?
>> Class: Key error.
>> Nina Zakharenko: Key error.
>> Nina Zakharenko: Got it? So two different ways of accessing items, one throws an explicit error, one does not.

>> Nina Zakharenko: The nice thing about get is, we can also provide a default value to return if the key is not present. So, looking at my dictionary of nums. What happens if I call nums.get1,
>> Nina Zakharenko: The number 1? nums.get|"four"|? Turns nothing one, right, or non-type. Now I can provide an optional argument to get.

And say, if this key is not present, return this default value instead.
>> Nina Zakharenko: Okay, so what if I call nums.get"one" but also provide a default value, which on will I get?
>> Class: Your 1.
>> Nina Zakharenko: Get 1, all right, so that says give me the value for that key if it's present.

If it's not, here's the default value instead, pretty nice handy little short cut.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now