1 00:00:00,600 --> 00:00:01,859 Instructor: Welcome back. 2 00:00:01,859 --> 00:00:05,430 I mentioned to you that a dictionary's values 3 00:00:05,430 --> 00:00:08,220 can hold any sort of data type. 4 00:00:08,220 --> 00:00:10,230 But what about the keys? 5 00:00:10,230 --> 00:00:15,230 Up until now, I've only used strings to denote a key. 6 00:00:16,410 --> 00:00:18,540 But could I do something like this? 7 00:00:18,540 --> 00:00:21,483 Could I do 123 like this? 8 00:00:22,920 --> 00:00:23,910 Well, let's have a look. 9 00:00:23,910 --> 00:00:24,910 Let's give it a try. 10 00:00:25,890 --> 00:00:28,143 If I do 123, 11 00:00:29,400 --> 00:00:30,483 and I click run, 12 00:00:32,580 --> 00:00:34,740 I get 1, 2, 3. 13 00:00:34,740 --> 00:00:35,913 Awesome, that works. 14 00:00:38,790 --> 00:00:40,083 What about True? 15 00:00:41,087 --> 00:00:46,053 If I have a key True and I click run, will that work? 16 00:00:47,520 --> 00:00:49,440 Yep, It looks like it works. 17 00:00:49,440 --> 00:00:50,820 What about a list? 18 00:00:50,820 --> 00:00:54,390 What if I have a list of let's say just 100? 19 00:00:54,390 --> 00:00:56,490 Would that work? 20 00:00:56,490 --> 00:01:01,080 If I click run, nope, it doesn't work. 21 00:01:01,080 --> 00:01:04,319 It says unhashable type list. 22 00:01:04,319 --> 00:01:05,403 What does that mean? 23 00:01:06,240 --> 00:01:10,443 Dictionary keys need to have a special property. 24 00:01:11,310 --> 00:01:15,210 A key needs to be immutable. 25 00:01:15,210 --> 00:01:18,840 That is a key cannot change. 26 00:01:18,840 --> 00:01:21,330 And numbers, Booleans, 27 00:01:21,330 --> 00:01:25,410 I mean, this is a value that cannot change. 28 00:01:25,410 --> 00:01:30,410 A string, if you remember, is a value that cannot change. 29 00:01:31,380 --> 00:01:32,580 It's immutable. 30 00:01:32,580 --> 00:01:36,330 But a list, if you remember, can be changed, right? 31 00:01:36,330 --> 00:01:39,840 On a list, I can reassign, 32 00:01:39,840 --> 00:01:44,370 let's say the index of zero to something else, right? 33 00:01:44,370 --> 00:01:46,987 So because of that, a dictionary says, 34 00:01:46,987 --> 00:01:48,720 "Hey, my keys, 35 00:01:48,720 --> 00:01:50,460 because I'm storing them in memory, 36 00:01:50,460 --> 00:01:51,750 and I don't wanna lose them, 37 00:01:51,750 --> 00:01:54,750 it has to be something that isn't going to change on me." 38 00:01:54,750 --> 00:01:56,040 Maybe a programmer comes in 39 00:01:56,040 --> 00:02:00,360 and by mistake changes this array of 100 40 00:02:00,360 --> 00:02:02,640 to have an index of something else. 41 00:02:02,640 --> 00:02:05,670 Well, dictionary doesn't really want that 42 00:02:05,670 --> 00:02:08,100 because it doesn't wanna lose this value. 43 00:02:08,100 --> 00:02:11,943 So a dictionary key always has to be immutable. 44 00:02:12,990 --> 00:02:15,300 And as we learn a few other things, 45 00:02:15,300 --> 00:02:18,960 like a tuple that we'll see in upcoming videos, 46 00:02:18,960 --> 00:02:21,420 you can use those as keys as well. 47 00:02:21,420 --> 00:02:26,420 However, most of the time, 95/99% of the time, 48 00:02:27,060 --> 00:02:31,410 a key for a dictionary is usually something descriptive, 49 00:02:31,410 --> 00:02:32,343 like a string. 50 00:02:33,480 --> 00:02:36,840 Okay, but what about this? 51 00:02:36,840 --> 00:02:40,203 What if we have another string? 52 00:02:41,280 --> 00:02:45,843 123, and let's just remove this for now. 53 00:02:48,090 --> 00:02:53,090 What happens when I search for 123, this string? 54 00:02:53,130 --> 00:02:56,343 If I click run, I get hello. 55 00:02:58,080 --> 00:03:02,370 A key in a dictionary has to be unique, 56 00:03:02,370 --> 00:03:05,280 because there can only be one key, 57 00:03:05,280 --> 00:03:07,350 because that key is gonna represent 58 00:03:07,350 --> 00:03:10,170 a bookshelf in that memory space. 59 00:03:10,170 --> 00:03:13,950 So anytime I do the same key, 60 00:03:13,950 --> 00:03:16,650 and maybe add a value, it's going to override. 61 00:03:16,650 --> 00:03:19,020 So this no longer exists. 62 00:03:19,020 --> 00:03:21,960 A key has to be unique, 63 00:03:21,960 --> 00:03:26,280 and it's something that can only exist, well, just once. 64 00:03:26,280 --> 00:03:29,850 Otherwise, we override it, which is why you see hello here. 65 00:03:29,850 --> 00:03:32,943 We've lost the array 1, 2, 3. 66 00:03:34,020 --> 00:03:37,020 Let's explore this idea a little bit more in the next video.