Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 9: Real Estate Analysis App
Lecture: Concept: Dictionaries

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's look at this core concept of dictionaries. Dictionaries are kind of like lists and they will store a bunch of different types of data for you,
0:09 but lists are very limited in the way you can get that data back, you can only get that back by index.
0:15 So I can get the third item, or the hundredth item or the minus one item
0:19 even to go and get the last out of the list but that's problematic in a lot of ways,
0:22 a much better way to store data if we are going to randomly access it is by some name that makes sense to people not just an index,
0:30 so here we have an information thing, maybe it talks about a person's age and the location, so here we have a dictionary
0:39 and we are setting the age to be 42 and the location to be Italy, so that first part we can kind of dynamically build it up,
0:45 but we can also use the two steps below right here where we can allocate and populate the dictionary all at once,
0:53 so those next two lines those are two ways to do the identical same bit of code to create a dictionary that has an age of 42 and a location of Italy.
1:02 When we want data out of the dictionary, instead of using a number, we just use a key in this case we would like that location information back
1:10 so we just say quote location, here we are using a string, it could be a number, it could be something much more complicated even
1:15 and that will actually give us the location back, that value is Italy. If you are not sure if a key is in the dictionary,
1:21 and you try to access it you will get a key error exception. So if we want to get the age value out of info and it's not guaranteed
1:30 that age is actually in there, you ask this somewhat non obvious but pretty cool way of saying if age in info and that will return true
1:38 only if there is some value for age inside of info.

Talk Python's Mastodon Michael Kennedy's Mastodon