Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Dictionaries are Python's version of associative arrays or hashed arrays. Dictionaries are created in a couple of different ways. We can create a dictionary with the curly braces and we can say key value like this. So now, we have a dictionary with those values in it. Actually, an easier way to create dictionaries is with its constructor because this allows you to use the named parameters feature of Python and it's just a whole lot easier to type. So you will see it done this way more often.
We get exactly the same result and see that that type is a class dictionary. Finally, if you want to, you can even initialize one dictionary from another. If I have another dictionary? And so there I have that dictionary. I can say d = dictionary and I can take these definitions from up here, copy those and paste those in, and then I can actually specify that other dictionary with the two asterisk notation and I get a dictionary that has all of that in it.
You can test if a value is in a dictionary by saying 'four' in x and I see that that's True and if I say 'three' in x that will not be true, because x here has just four, five and six in it. Likewise, you can iterate over a dictionary. You can say four k in d: print (k) and that will print out all of the keys. If you want to, you can say for k key and value in d.items print k and value.
And that will give you both the keys and the values all in one fell swoop. If you want to find a particular item in a dictionary, one way to do it is to simply subscript it. The problem with that is that if your dictionary does not have that key, you will get an exception. If I try x sub 'three', I will get an exception. The way around that is to use the get method. If I say x.get('three'), it will simply silently give me the none value.
If I say d.get('three') then I get the value. Another benefit of this is using the get method I can give it a default value. I can say give me that value of three or give me some default value. And I get the default value. Here I typed in the text 'not found'. If I simply want to delete an item from a dictionary, I can just look at the x here. I've got those items there. I can say del x' sub four' and now that's gone or I can say x.pop('five') and it will give me the value and delete it at the same time.
So those are the major functions of dictionaries. Dictionaries are tremendously useful. Whenever you need to organize your data in such a way that it's easy to find, you can certainly store any kind of data in a list or in a tuple. But the indexes are always going to be numeric. The advantage of the dictionaries is that the indexes are data. So you can index it on any kind of data. Usually, you will use text, usually you will use words, but this allows you to basically create your own named spaces and organize them.
They can be hierarchical. You can store dictionaries within dictionaries, you can store lists and tuples within dictionaries, and you will find as you will look through the examples of the code that I have written, I will often times carry one dictionary in a global namespace and put all of my data inside of that and then it becomes manageable. It's like a halfway point between flat data and an object with methods. Obviously, there is going to be times you are going to want to create an object with methods to handle your data in very specific ways, but for just organizing a whole lot of global variables or flags, you will find that dictionaries are very useful and for organizing object data within an object, you will find that dictionaries are very, very useful and very, very commonly used in these kinds of situations.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 92594 Viewers
82 Video lessons · 104156 Viewers
71 Video lessons · 75697 Viewers
56 Video lessons · 107087 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.