1 00:00:00,760 --> 00:00:07,120 Let's talk about our next data structure, and this one is called a dictionary. 2 00:00:08,790 --> 00:00:13,770 In other languages, it might be called the hash table, maybe map or objects. 3 00:00:14,800 --> 00:00:22,840 In Python, we have this idea of dictionary or Python likes to call it DECT, have to make sure I pronounce 4 00:00:22,840 --> 00:00:23,460 that properly. 5 00:00:23,470 --> 00:00:28,680 That's a really hard word to say when you're recording and you're repeating it over and over anyway. 6 00:00:29,140 --> 00:00:31,210 Dictionary, what is it? 7 00:00:31,630 --> 00:00:35,650 Well, it's a data type in Python, but it's also a data structure. 8 00:00:36,250 --> 00:00:40,380 Remember that I mentioned this idea of data structure when we talked about lists. 9 00:00:40,930 --> 00:00:45,280 There were the first the data structure in Python that we learned. 10 00:00:45,730 --> 00:00:53,800 It's a way for us to organize our data in a form that is has some different pros and cons and how we 11 00:00:53,800 --> 00:00:54,340 access it. 12 00:00:54,550 --> 00:00:59,380 For example, with lists, we saw that they're easily ordered. 13 00:00:59,380 --> 00:01:02,470 We can access them through indexes like zero one. 14 00:01:02,470 --> 00:01:03,730 We can reverse them. 15 00:01:03,730 --> 00:01:04,960 We can insert into them. 16 00:01:05,170 --> 00:01:06,440 It was really, really nice. 17 00:01:06,910 --> 00:01:08,050 What about dictionaries? 18 00:01:08,800 --> 00:01:10,900 Well, let's see what a dictionary looks like. 19 00:01:11,320 --> 00:01:12,220 A dictionary. 20 00:01:14,090 --> 00:01:17,540 We'll look something like this will have a key. 21 00:01:18,590 --> 00:01:19,700 And a value. 22 00:01:20,810 --> 00:01:23,100 Then another comma and then another key. 23 00:01:24,500 --> 00:01:26,180 And another value. 24 00:01:27,590 --> 00:01:35,690 Let's decipher this, so I'm using curly brackets here, which denotes a dictionary and unlike a list, 25 00:01:35,870 --> 00:01:45,470 we have what we call a key value pair, a key is a string for us to grab the value. 26 00:01:46,670 --> 00:01:47,240 What do I mean? 27 00:01:47,540 --> 00:01:52,100 Well, in order for us to access any of these values, I would go dictionary. 28 00:01:53,530 --> 00:02:00,370 And then say the key in our case, let's say we want to grab we would grab the key and then when we 29 00:02:00,370 --> 00:02:01,990 print here the result. 30 00:02:04,730 --> 00:02:10,900 We get the value to we're essentially telling the computer, hey, I have this valuable dictionary, 31 00:02:10,910 --> 00:02:15,110 remember, I can mean this whatever I want, I have this variable dictionary. 32 00:02:15,230 --> 00:02:21,780 I want you to go find the key B and this key B if it exists, grab me the value. 33 00:02:21,830 --> 00:02:25,130 So it's going to go into your memory or into the machine's memory. 34 00:02:25,340 --> 00:02:30,310 It's going to find where B is stored in our memory and grab the value. 35 00:02:30,410 --> 00:02:37,610 So in the bookshelf, B, we have the value to what if we do see which doesn't exist. 36 00:02:37,760 --> 00:02:40,550 If I click run now I get an error. 37 00:02:40,610 --> 00:02:44,630 It tells me, hey, sorry, you're looking for Kissi, but I don't really have that key. 38 00:02:45,260 --> 00:02:47,390 And this is what a dictionary is. 39 00:02:47,690 --> 00:02:51,830 A dictionary is an unordered key value pair. 40 00:02:52,500 --> 00:02:57,860 What I mean by that unordered means that they are not right next to each other in memory. 41 00:02:58,070 --> 00:03:05,120 Remember, with lists we could access lists with index of zero, an index of one, index of two, all 42 00:03:05,120 --> 00:03:10,970 those bookshelves or right next to each other, a dictionary on the other hand, they're all over the 43 00:03:10,970 --> 00:03:11,360 place. 44 00:03:11,540 --> 00:03:17,600 Although I've put A and B here, this might be in one spot, a memory in this one, in another spot 45 00:03:17,600 --> 00:03:18,110 in memory. 46 00:03:18,830 --> 00:03:23,540 And if I added, let's say X, this will be in another spot in memory. 47 00:03:24,390 --> 00:03:27,550 And this might not be in order. 48 00:03:27,660 --> 00:03:35,250 Maybe I've written it a, B, X, but when I actually return the entire dictionary, maybe I don't have 49 00:03:35,250 --> 00:03:35,850 this in order. 50 00:03:36,210 --> 00:03:38,640 These are all just scattered all across our memory. 51 00:03:39,830 --> 00:03:45,230 As you see here, when I received the dictionary, I get these things in order, but that's only because 52 00:03:45,230 --> 00:03:45,760 it's small. 53 00:03:45,770 --> 00:03:51,350 If I had a really, really large dictionary, I might not have them in order that I've inserted it in. 54 00:03:52,450 --> 00:04:01,030 So a dictionary is an unordered key value pair, and as long as we know the key, that is whatever the 55 00:04:01,030 --> 00:04:07,300 key that we're looking for, then we just give that and our computer is going to know, hey, where 56 00:04:07,300 --> 00:04:09,730 in memory to look to grab the values. 57 00:04:10,890 --> 00:04:17,070 Now, here's the cool thing about data structures like dictionaries and lists their containers around 58 00:04:17,250 --> 00:04:18,240 data, right? 59 00:04:18,450 --> 00:04:21,360 So this can be anything that we want. 60 00:04:21,570 --> 00:04:23,820 Let's say we want this one to be a list. 61 00:04:23,820 --> 00:04:25,350 One, two, three. 62 00:04:25,800 --> 00:04:28,080 Maybe we want this to be a string. 63 00:04:28,620 --> 00:04:29,120 Hello. 64 00:04:29,460 --> 00:04:32,340 And maybe this is a boolean value like. 65 00:04:32,340 --> 00:04:32,760 True. 66 00:04:33,960 --> 00:04:38,870 That is completely valid, I can access the A. 67 00:04:40,030 --> 00:04:46,960 Like, so and I get the list, what if I want the second item in the list while I do index of one like 68 00:04:46,960 --> 00:04:47,290 this? 69 00:04:49,480 --> 00:04:56,050 And there you go, I get to and this is the same with lists and you'll see this a lot, where if we 70 00:04:56,050 --> 00:05:06,460 had a list or let's call it my list and my list contains a dictionary that has, let's say a. 71 00:05:08,030 --> 00:05:08,660 One. 72 00:05:09,730 --> 00:05:12,850 And you know what, let's just copy and paste this, we'll do this. 73 00:05:14,190 --> 00:05:16,290 So we'll have the first item in the array. 74 00:05:19,200 --> 00:05:23,040 Like this, then comma, then another item in the array. 75 00:05:25,290 --> 00:05:28,920 And this time around, this one has four, five, six. 76 00:05:30,660 --> 00:05:33,420 So that I can now grab from my list. 77 00:05:37,110 --> 00:05:38,610 First item. 78 00:05:39,890 --> 00:05:40,580 Indira. 79 00:05:42,080 --> 00:05:44,030 And then maybe grab. 80 00:05:45,360 --> 00:05:52,950 The A that is the akey, so remember, this is a string and that we want to grab, let's say, the third 81 00:05:52,980 --> 00:05:54,660 item, so we'll go. 82 00:05:56,590 --> 00:06:00,970 To fight run this, I should get three and two. 83 00:06:02,230 --> 00:06:02,750 There you go. 84 00:06:03,580 --> 00:06:08,800 Now, you might be asking yourself, hmm, what's the deal with these data structures? 85 00:06:08,800 --> 00:06:10,740 Like, why do we need a dictionary? 86 00:06:10,750 --> 00:06:12,280 Why do we need a list? 87 00:06:12,280 --> 00:06:14,250 Why can we just have one thing that does everything? 88 00:06:14,260 --> 00:06:15,400 Isn't that simpler? 89 00:06:16,300 --> 00:06:20,140 Think about that, because in the next video, I'm going to try and answer that question for you.