Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 9: Real Estate Analysis App
Lecture: Dictionary playground

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's put our app aside for just a minute and talk about dictionaries We can create dictionaries in a variety of ways
0:07 and depending on how you want to store data, one way makes more sense than the other.
0:12 The idea of a dictionary, it's kind of like a list that you can put items into it and then get them back by looking them up,
0:19 but instead of using numbers like list of 7 you actually use something called a key, now those keys can be strings,
0:27 they can be numbers or they can be a variety of objects, whatever the natural thing that you use to identify something with
0:34 you can use that as the index if you will, instead of a number. So let's create a dictionary, and there is several ways to do it,
0:42 we could say lookup= {} this if you have open close curly braces that's a dictionary,
0:48 you could write the identical code say look up= dictionary, like this,
0:54 or you could even initialize it, like this lookup = {'age': 42, 'loc': Italy}, right,
1:04 and we could use this there is actually a variety of ways to do this we could say basically the same thing dictionary,
1:10 but instead of using strings, we can use named parameters, keyword values like so, almost, like so.
1:19 So I could print look up here, you can see the sort of Python print out style is this, if we have a dictionary though we can get the values back,
1:28 if I wanted to know the location I could say give me lookup of location, and instead of using numbers, I say give me the second thing,
1:36 it doesn't matter where or what order this is in, in fact these are unordered collections, but using the extremely fast algorithm to find this value,
1:45 so here you can see the location is Italy. If we try to get something out of the lookup that doesn't exist,
1:51 if I try to ask for the category, it's not super happy, if you are going to do this you have to do a somewhat non obvious but kind of cool syntax,
2:01 you have to say if cat is in lookup, if the key is in there, then you can print out cat.
2:07 So if I run it right now, there is no cat in here, so there is no output, but I can add cat dynamically later after I create
2:14 I can say cat and I just set a value is fun code demos and now when I run it, hey cat is in there and its value is this.
2:24 Sometimes you use dictionaries to store data like this, right, sometimes you store heterogeneous data I want to take an individual piece of information
2:35 and say the age is this, the location is that, in fact whenever we create a custom class, if we have a class like this,
2:42 remember the wizard, and we defined the dunder init, and we said it's going to take a name and a level, and we said something like this,
2:53 this actually creates dictionary entry when the dictionary key values level and then the value is the name of it,
3:02 so dictionaries are actually super central to object oriented programming, let's create a wizard really quick, now if I print out Gandolf
3:12 you will see there is not much going on right here is Gandolf right here, main wizard,
3:17 but there is an implicit dictionary that actually stores this data and you can see the level and the name, right,
3:24 so understanding dictionaries is ultra important in Python because it's not just about an interesting data structure, you use sometime,
3:30 in fact all objects are built on the concept of dictionaries so you really have to understand them well.
3:36 One use case of this is sort of storing in an individual item disparate data like so like level name or age and location.
3:45 Another one might be you have a bunch of similar data like what if I got a 100,000 users back from a database query or from a web service,
3:52 and I'd like to be able to quickly pull them back by ID, almost like a database, like in memory I'd like say give me user 2,711, bam,
4:02 and I want that back instantly, not seeking over an index, not seeking over a list and doing 2,000 comparisons and then finally going
4:09 oh here is the one you were looking for, but instantly finding it basically. So I have a little bit of code I stash here, look at that,
4:15 so here we have our users and this is, imagine we have many, many more of them and we would like to be able to pull them back by their id,
4:24 so we can also use dictionaries for this, let's say lookup = new dictionary again, and we can just say for each u in users we can say it to look up
4:34 and I would like to when I am going to look up a user maybe I want to look them up by id, so I can just say this,
4:40 any time I want if I have a million users if I have their id I can get them back ultra fast
4:45 so I want to know what the user with id 3 looks like, we'll let's run it, bam,
4:50 that's user 3 with email user3@talkpython.fm. we could use a different index, we could use email, now if I run this it's going to crash,
4:59 I say I want the user with the key of that email address boom, there is that user right there. So we can look them up by whatever we want,
5:08 if we can say we would like to have a bunch of users and be able to ultra fast pull out the one by you name it,
5:14 we could do user name, we could do email, we could do id, you can only do one but this lets us store
5:19 homogeneous sets of data but then find them by key or as up here,
5:23 dictionaries were using more for storing disparate data, not different instances of it,
5:28 but actually different but related data like both age and location are somehow tied together, we can access the part.
5:36 It's more this top style that we are going to use for our csv file, so let's take a moment, look at this core concept,
5:41 and then we'll go back to working on our real estate data mining app.


Talk Python's Mastodon Michael Kennedy's Mastodon