Practical Guide to Python Dictionaries
Transcript from the "Dictionaries" Lesson
>> Next up, we have dictionaries. Dictionaries are a data type that allow us to store key and value pairs. Dictionaries themselves aren't mutable, but just like sets, dictionary keys is can only be immutable. Why? Because dictionary keys need to be hashable. Dictionaries allow fast item lookup and fast membership testing you can quickly say, given a key is this key in my dictionary or not.
[00:00:36] And that's because the hash of the key is compared with the hash of the keys in the dictionary, don't have to look through it each item in that dictionary and say, are are you the item, are you the item, are you the item that I'm looking for? So can I have a list as a dictionary and a key, or I'm sorry, a list as a key in the dictionary?
[00:00:56] If I ask that question correctly, I would get a better answer. No. Right. So a list is mutable dictionary keys can only be immutable. We cannot have mutable data types as dictionary keys. Can we have a tuple as a dictionary key? Yes. Tuples are immutable, so we can use them as a dictionary key.
[00:01:23] I have a dictionary cheat sheet here for you. But let's go through some examples of what dictionaries in Python look like. So if sets had curly braces, dictionaries have curly braces and colons and that colon is how you differentiate between the key and the value. If I wanted to create an empty dictionary, how do I do that?
[00:01:58] We already saw that.
>> Two curly braces
>> Just two curly braces. If I wanted to create dictionaries with items in them, I would now have to define keys and values. So. And separate them with commas. So the key is on the left hand side of the colon, the value is on the right hand side of the colon, separated with commas.
[00:02:30] And that has now given me a dictionary with two key value pairs in it. I can go ahead and assign this to a variable and then check its type. And that will show me that it is a dictionary. Now, dictionary keys need to be immutable. Dictionary values do not have that same restriction.
[00:02:59] So you can say my_numbers are a dictionary and say, the key is one and the value is this list. That is totally fine. Because when I want to retrieve this item from my dictionary, I'll be doing it by its key. [COUGH] Now, dictionaries, like sets do not have an order in my dict, there is no zeroeth item or first item.
[00:03:41] If I try to access the zeroeth item, I'm going to get an error saying I expect to key and that key does not exist. So to access items in a dictionary, you use the square bracket notation but instead of providing an index like you would with a list or tuple, you instead provide a key.
[00:04:07] And now that's going to get the value from my dictionary that has the key of one. If I tried to get a value that doesn't exist again, I got a keyError. If I wanted to get something from my dictionary and not deal with an exception that I then have to catch, I can use the get method and this will not throw an error if that key does not exist.
[00:04:42] The get method has a second optional argument that is a default value. So I can say, if this key does not exist, return this default instead. If I pass in a key here that does exist, what will I see? Will I see default? No, I'm going to get back the number two, from my dict because that key exists, therefore the default is not relevant.
[00:05:19] To add and remove items from dictionaries, you can use that same notation that we used to update items and lists but instead of passing in a position, you're going to pass in a key. So if I wanted to update the dictionary at key two. I could use this notation.
[00:05:46] And now my dict has that very silly long string in it. I could use the same notation for adding items to the dictionary that don't already exist. And now I have a new key in there. Just like with sets, you could update, you can call the update on a dictionary and pass in another dictionary and that will combine the two dictionaries together.
[00:06:16] So whatever dictionary you're calling the update on will be the one that is modified. You can have pretty complex dictionaries, remember a dictionary can have a list as a value. Let's say this key is one and the value is a list. If I call my_nums and pass in this key of one, I'm going to get back a list.
[00:06:55] I can call .append on a list, which means that I can just chain these actions together. So if I know that this returns a list, I can call .append on it and append to this list just like I normally would. Now, if I look at my dictionary, Oops!
[00:07:24] And I spell it correctly we'll see that I have upended a list to my list. So dictionaries can get very complex, the values in them can be objects. So know that when you look at them you might have to break down which type is which in the value and be mindful of, curly braces, parentheses, square brackets to figure out what type is what.
[00:07:53] If you want to work with the. Values in a dictionary. There are three methods that you need to know about. One is .keys, that will get you all of the keys. One is .values, that will get you all of the values but the most important one that you're going to use most often Is my_dict.items and that's going to return all of the key and value pairs.
[00:08:29] When you're going to try to loop over a dictionary, you might expect that what is actually returned is going to be the items the key value pairs but unfortunately, that is not correct. When you loop over a dictionary with actually returned is just the keys. So something to be mindful of, if you want to loop over a dictionary, which we'll look at later, you want to make sure that you call .items on it to get both the key and the value pairs.
[00:09:00] So just a quick thing about mutability, the simple types in float, decimal, string, Boolean, tuple, those are immutable. Lists, sets, dictionaries are mutable. So if you don't remember, take a look at the mutability cheat sheet. For the time being, we are going to skip over the practice for this chapter and go through it with the next chapter.