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

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