In addition to stacks and queues, we can also use one of my favorite Python data types: the dictionary. A dictionary is basically a collection of key-value pairs that makes search and retrieval of values by key quite simple.
1.Introduction2 lessons, 11:32
2.Python Building Blocks6 lessons, 1:08:07
3.Controlling the Flow7 lessons, 1:20:10
4.Common Data Structures4 lessons, 46:49
5.Application Structure7 lessons, 1:15:12
6.Collections7 lessons, 46:55
7.File I/O6 lessons, 48:51
8.Networking5 lessons, 43:48
9.Connecting to Network Services3 lessons, 34:27
10.Conclusion1 lesson, 02:08
In the previous lesson we talked a little bit about lists, stacks and queues,and then ultimately how to transform lists into stacks and queues and maybe some basic use cases of when you might want to use them. And that's great and they're very useful, but in my day to day activities regardless of the language or application that I'm working on, it's a fair statement to say that I probably use dictionaries, the particular data structure that I'm going to discuss in this lesson, pretty much everyday. It's one of my favorites, it's one that I use quite often and I just find that it's very handy for a lot of different applications. So let's start off by discussing what exactly a dictionary is. So a dictionary is what is known as a mapping type. And it's basically just a collection of key value pairs, if you want to think of it that way. So if you imagine that you needed to find the definition of a specific word, regardless of the language you're in, you would find a dictionary that has the, [LAUGH] or maybe once upon a time you would find a dictionary, and you wouldn't just necessarily go to Google. But you would find a dictionary for your language and you would open it to the page that would have the word that you were looking for and think of that word as the key. That's the key that you are using to try to find a piece of information. And then once you have found that particular key, the value associated with it would be in this case, the definition or whatever other associative piece of information is that you're actually looking for. So how do we create a similar data structure to that physical concept within Python. Well it just so happens that there is a built in construct to do something very similar. So let's go ahead and start to create this basic concept of a dictionary. Now once again as I said, dictionaries are made up of key value pairs. So how do we do that? Well, to begin with the key value pair begins with some sort of key and then a value, but where do we put them, what sort of syntax do we use to define this collection and the chosen syntax used within Python and in honestly in a number of other languages is the open enclosed curly braces. So when we do this, this is going to specify that we are creating a dictionary. Let's just say at this point we were creating a dictionary of say ages, people's ages. Now we would create once again, a variable name, we'll call this ages, and we'll set of equal to open and closed parentheses, and that's it. [SOUND]. Just by doing that we have told Python that we want to create a dictionary. If we ask for ages back, it's going to give us this empty dictionary or empty collection of name value pairs. Let's go ahead and say that I wanted to put something in there. I'm just gonna arbitrarily pick some [LAUGH] random names and assign ages to them, and these may or may not be accurate and it doesn't really matter. But it's just simply here to prove a point. If we wanted to add a new name value pair to this ages collection, we would simply specify ages. And then in a previous example, in a number of other data types like strings and lists, and ultimately stacks and queues, we used the open close square brackets to really denote a location within those data structure, and in dictionaries it's fairly similar. But in this case what we want to do is we want to tell it which key we want to assign some data for. So let's just pick an arbitrary name and we'll just say here, John. Now John is going to be our key, and that's what's denoted here, by these square brackets up against the ages variable name. Then in order to assign a value or in this case an age to John, we would simply say that is equal to 25. So now if I were to say please give me back ages you can now see that this dictionary consists of one key value pair where the first key is John and the value associated with John is 25. So, let's do another one. We'll say this time this is going to be Daniel, and Daniel is going to be equal to 30. So now let's go ahead and take a look at ages. Now we can see that we have multiple key value pairs within this dictionary denoted or separated by a comma, so how do we get back a specific value or a specific age in this case that's associated with a key. Well, once again we would simply specify the name of the dictionary and then we provide a key. So in this case I'll say John. And as you can see here we get back 25. That is pretty good, so how do we then maybe override something? Let's say John had a birthday. We would say ages. John is now equal to 26. So if I were to ask for ages back again, we get 26. As you can see here, a dictionary is a mutable object in and of itself because I can change things. I can change the values associated with keys, and so on and so forth. Now, what do you think might happen if I were to ask for the age of a person that's not in my dictionary? Well, I would say ages and let's say I had asked for Derek's age, and I'm going to get an error because the key doesn't actually exist. This is something to bear in mind in that when you're working with these dictionaries, if I ask for the value associated to a key that does not exist, I'm going to get an error. Well, that's kind of a bad thing, wouldn't it be? Isn't there a better way that I could maybe check to see if a particular key exists first? Well, you absolutely can. So, how could I get back maybe just a list of keys that are in my dictionary to see if something actually exists. Well, there just so happens to be a function called keys on a dictionary where it's going to give you back this kind of strange object here of dict_keys, which it's going to give you ultimately the list of keys that are associated. But if I just wanted to work with this a little bit easier I could use the list function and then I could ask for ages.keys using that function. And then the return value here is going to be a simple list of these keys, so then I can start to actually work with those keys instead of actually having to deal with that other sort of strange data type. Now what if I wanted to do the opposite? What if I wanted to get all of the values from a dictionary and do something with it? So let's say I wanted to maybe loop through all the ages of the guests at a party, and take an average so I could see what the average age of the people were at the actual party. Well I could do something very similar, I could say ages.values. And it's gonna give me back a similar data structure, only this time it's dict_ value. So if I did the list function again and said ages.dot values, then I would get back the values associated with all the keys found within the dictionary. So that's fine, and I'm getting back these nice little lists but what if I actually wanted to check. How do I check to see if something's in there. There's gotta be an easy construct to be able to figure out if a particular key or a value exists in my dictionary, and it just so happens there is. So, just before like I looked for Derek and I got this error. I could do something like this. I could say Derek in ages. And this in keyword is going to look in ages to see if there's a key associated with Derek. And if I were to hit Enter, you would see here that we get back false. This is a nice little check you can do before you start to go ahead and just dig blindly into your dictionary looking for keys. You could do first an if statement to see if a particular key exists in my dictionary, and I'll either get back a true or a false. So those are the basic operations that you're gonna find that you can use with a dictionary. But before I let you go, I would like to show you one other basic way that you can begin to create dictionaries that might be kind of useful depending on what kind of data you're looking for or what kind of data you're creating, or if you're just creating maybe a bunch of test data that you can pump through an application of yours. There's a nice little function called dict, D-I-C-T, and then this will take in as its arguments, some keyword arguments similar to what we saw in some of our examples in the two lessons about functions. I can start to create some name value pairs right here, so let's say I call this new ages, and I set that equal to, and this time will put in here, Derek is going to be equal to 30 and Katie is going to be equal to 29. And I can put a number of other things in here. Brian is equal to 5 and so on and so forth. And now I'm gonna get back a new dictionary so I can check for new ages. And as you can see here what it did is it took those keyword arguments, Derek, Katie and Brian, turned them into strings, and then made those values the keys within my dictionary and then assigned the values that I set them equal to as the values in my dictionary. So it's a nice, little, quick way to start to create dictionaries. If you really want to be able to create them very quickly and you know what the keys and the values are going to be, and if you're not necessarily going to need to dynamically create them. Now that you've learned about these additional data types within Python, one of the next, more powerful things that you're going to want to be able to do is you're gonna want to be able to cycle through these collections, or loop through them and there's gotta be powerful constructs within Python to be able to do that, and surprise, surprise there is. So in the next lesson, we're gonna talk about some looping techniques that are going to allow us to efficiently and very quickly get through the values and the data that is found in dictionaries.