Move from Excel to Python with Pandas Transcripts
Chapter: Appendix: Python language concepts
Lecture: Concept: Dictionaries

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Dictionaries are essential in Python. A dictionary is a data structure that very efficiently stores
0:08 and can rapidly look up and retrieve items by some kind of key. You can think of this as kind of a primary key in a database
0:15 or some other unique element representing the thing that you want to look up.
0:19 Dictionaries come in a couple of forms, the form you see on the screen here we put multiple related pieces of information together that we can lookup,
0:28 so here maybe we have the age of a person and their current location. Other types of dictionaries are maybe long lists of homogeneous data
0:36 maybe a list of a hundred thousand customers and you can look them up by key which is say their email address, which is unique in your system.
0:44 Whichever type you are working with, the way they function is the same. We can create dictionaries in many ways, three of them here are on the screen;
0:50 the first block we initialize a dictionary by name and then we set the value for age to 42, we set the location to Italy.
0:57 We can do this in one line by calling the dict initializer and pass the key value argument, we can say dict age and location
1:05 or we can use the language syntax version, if you will, with curly braces and then key colon value, and it turns out all three of these are equivalent,
1:14 and you can use whichever one makes the most sense for your situation, so here the created and then populated,
1:19 here created via the name and keyword arguments or here created via the language structures.
1:25 The fact that this is so built-in to the language to tell you dictionaries are pretty important.
1:29 Now, if we want to access an item, from the dictionary, we just use this index [ ] and then we pass the key whatever the key is.
1:37 In this case, we are using the location or the name of the property we are trying to look up so we are asking for the location.
1:44 My other example if we had a dictionary populated with a hundred thousand customer objects,
1:48 and the keyword is the email address, you would put in the email for the specific customer you are looking for.
1:54 Now, if we ask for something that doesn't exist, this will crash with a KeyError exception,
1:58 so for example if I said "info['height']", there is no height, so it will crash. there is a wide range of ways in which we can get the value out
2:06 or check for the existence of a value, but the most straightforward is to use it in this "in" operator,
2:11 so here we can test whether age is in this info object we can say "if age in info" and then it's safe to use info of age.
2:19 So this is just scratching the surface of dictionaries, you'll see that they appear in many places and they play a central role
2:26 to many of the internal implementations in Python, so be sure to get familiar with them.

Talk Python's Mastodon Michael Kennedy's Mastodon