# #100DaysOfCode in Python Transcripts Chapter: Days 7-9: Python Data Structures Lecture: Creating and Parsing Dictionaries

0:00 Alright. Next up we're going to talk about dictionaries, or dicts for short, let's keep saying dictionaries, just to be safe.
0:10 And they're made up of pretty much two different things. The easiest way to demonstrate it is to just create one in its simplest form.
0:20 So we'll create a dictionary called pybites, and let's just add a few things to it. The first thing we need to add is a key,
0:30 and the second thing we need to add is a value. Alright. Looking at it here, there's a separator here, your colon.
0:38 And that separates your key, the key comes first, from your value on the end. Alright, and this here is technically a dictionary.
0:49 It may only have one item in it, but it's a dictionary all the same. Let's make it a little more interesting.
0:55 When you're adding a second item into your dictionary, you separate it with a comma. Alright, so we've got Julian, now let's add Bob,
1:06 and let's just say he's 33, then we have Mike, let's say he's also 33. Let's just say I'm being super generous with those ages
1:16 guys, anyway, there's our dictionary. So to view what it looks like, we just type in, pybites. And there it is there, the three different items.
1:32 And these link to each other. So the key is Mike, the value is 33. The key is Bob, the value is also 33.
1:41 Now notice when we printed it out, it printed out in this order. Well this order is not explicit. With a dictionary, when you're passing it,
1:54 when you're listing it out, when you're displaying it, there's no guarantee that the data is going to remain in the order, okay?
2:04 That's very important to remember with dictionaries, they are unordered, okay? Now let's say we wanted to create a dictionary
2:16 but we didn't know what values we were going to put into it from the start. We'll just do a quick little side track demonstration here.
2:23 You would start off just as you would do with a list that's empty, except you're using two curly brackets instead of the square ones.
2:33 So people is now an empty dictionary. There's absolutely nothing in it. To add someone to it, this is the tricky part, this is where
2:42 it gets a bit different. What you need to do, you need to actually pretty much in square brackets, you need to choose the key, alright?
2:53 In this instance the key for a list of people is going to be Julian, and we're going to assign that key a value of 30.
3:02 So the dictionary people, we're creating an entry, a key, of Julian, and we're assigning it the value of 30.
3:12 We list out people, there we go, we see that exact same formatted dictionary as we did when we explicitly defined it up here, okay?
3:25 We can add to it again. We can go, people, Bob, and we can assign Bob the age this time of 103. And there we go.
3:43 So same thing, right? This time we just populated an empty dictionary. But either way it all comes out the same.
3:52 Now the way you interact with the dictionaries is a bit different to lists. The way we view just the keys, just these keys here,
4:02 forget the values for a minute, is we use keys. So we can go pybites.keys, and there are our dictionary keys. Julian, Bob, Mike.
4:15 The same things for the values. pybites.values, and there we go, the three values. 30, 33, and 33. Now what if we wanted to see all of this?
4:27 Now this is all well and good because we're in the shell. But if this was an actual application you can't just
4:32 type in the name of your dictionary and expect it to print out, right? This is just all standard out through the shell.
4:40 So the way we would actually print out, first of all, the way we actually see each pair of dictionary items is to use items().
4:54 We can see Julian 30, Bob 33, Mike 33. These are our three different dictionary key value combinations or items. Now how do we pass over all of that?
5:09 Well we do it just how we would with a normal list. We can go for keys in pybites.keys, print keys. There we go.
5:25 We can do for values in PieBites.values, print values. Okay? This is all very similar, you can see the similarities between dictionaries and lists.
5:43 But last but not least, what if we want to print out all of this information, not just keys, not just values,
5:49 without having to run two separate for loops, which would be quite un-Pythonic right? Well, God love Python, we can go for keys values in
6:03 pybites.items(), so for keys and values in PieBites.items Print keys, and remember we have to do the string thing here, values, and there we go.
6:19 Julian 30, Bob 33, Mike 33. Now obviously that is not very pleasant on the eye, so we can do our standard string formatting.
6:29 So we can go, for keys, values in pybites.items(), print, string is digit years of age, keys, values, okay?
6:54 And there we go. We can see Julian is 30 years of age, Bob is 33 years of age, Mike is 33 years of age. And that's dictionaries.
7:02 It's actually that simple to iterate over dictionary, and that's pretty much it. You've got your key, and your value.
7:10 And you just do it over and over again, just like a list. So enjoy your dictionaries, and get used to them, because you'll be playing with them.

Talk Python's Mastodon Michael Kennedy's Mastodon