1 00:00:00,058 --> 00:00:03,513 In this video, we're gonna talk about how to access keys and 2 00:00:03,513 --> 00:00:05,897 values from inside a Python dictionary. 3 00:00:05,897 --> 00:00:09,449 Do you remember how I mentioned previously that unlike sequences, 4 00:00:09,449 --> 00:00:13,265 which are indexed by numbers, dictionaries are indexed by their keys? 5 00:00:13,265 --> 00:00:17,131 We can use that difference to transfer what we know about accessing values and 6 00:00:17,131 --> 00:00:19,820 sequences, to accessing values and dictionaries. 7 00:00:19,820 --> 00:00:22,866 You see here in my workspace, I have a simple Python list. 8 00:00:22,866 --> 00:00:26,668 Without any context, the data stored in this list doesn't make a lot of sense, 9 00:00:26,668 --> 00:00:29,585 which is why soon, we're gonna convert it to a dictionary. 10 00:00:29,585 --> 00:00:33,579 But, for right now, let's refresh how we get values out of this list. 11 00:00:33,579 --> 00:00:38,339 If I want the first element in the list, I'll use the code course, 12 00:00:38,339 --> 00:00:43,269 then square brackets, and then the index of the value I want to grab, 13 00:00:43,269 --> 00:00:45,568 in this case, let's say zero. 14 00:00:45,568 --> 00:00:48,855 The same syntax is used when pulling values from dictionaries. 15 00:00:48,855 --> 00:00:52,918 Except instead of putting a numerical index inside the square brackets, 16 00:00:52,918 --> 00:00:54,172 we put the values key. 17 00:00:54,172 --> 00:00:57,967 So first let's back up and convert this list to a dictionary. 18 00:00:57,967 --> 00:01:02,127 To do that, I'll first change the square brackets here to curly braces. 19 00:01:04,815 --> 00:01:08,952 Then for each value in this I'll add a key, which is kinda like a label 20 00:01:24,459 --> 00:01:26,421 Now to access that first value again, 21 00:01:26,421 --> 00:01:29,685 I'll just change this code here to reflect the correct index. 22 00:01:32,903 --> 00:01:35,505 And now I'll wrap that in a print statement, and then I'll save and 23 00:01:35,505 --> 00:01:36,748 run the file to show the output. 24 00:01:40,792 --> 00:01:41,898 Let's save. 25 00:01:51,768 --> 00:01:53,851 Yep, it printed out Ashley. 26 00:01:53,851 --> 00:01:57,598 So that the basics of accessing an individual value from a dictionary. 27 00:01:57,598 --> 00:02:01,143 It's very similar to accessing an individual value from a sequence. 28 00:02:01,143 --> 00:02:05,434 It's important to know that if you attempt to access a value with a non existing key, 29 00:02:05,434 --> 00:02:08,288 Python won't just skip over it, you will get an error. 30 00:02:08,288 --> 00:02:11,892 Now, Python also has ways to grab a list of all the keys or 31 00:02:11,892 --> 00:02:14,138 all the values from a dictionary. 32 00:02:14,138 --> 00:02:18,319 And even a built-in function to sort those lists into lexicographical order. 33 00:02:18,319 --> 00:02:21,104 You might have learned about lexicographical ordering before, but 34 00:02:21,104 --> 00:02:22,839 check the teacher's notes for more info. 35 00:02:22,839 --> 00:02:26,172 So let's move down to the terminal where we can work a little more efficiently with 36 00:02:26,172 --> 00:02:26,912 our dictionary. 37 00:02:26,912 --> 00:02:29,308 I'm gonna start the interpreter and copy the dictionary down there. 38 00:02:38,123 --> 00:02:41,751 So, first, to get a list of all the keys in the dictionary, 39 00:02:41,751 --> 00:02:43,430 I'll use the keys method. 40 00:02:43,430 --> 00:02:47,387 To use that I type the name of the dictionary which is course then dot and 41 00:02:47,387 --> 00:02:50,404 then keys, the name of the method, and then parens, 42 00:02:50,404 --> 00:02:52,439 just like a regular function call. 43 00:02:52,439 --> 00:02:56,037 When I hit Enter, it will show the return value from the keys method. 44 00:02:58,203 --> 00:03:02,415 Yep, here's a list of every key, teacher, title and level. 45 00:03:02,415 --> 00:03:07,363 Similarly, we can access a list of every value in the dictionary using the values 46 00:03:07,363 --> 00:03:07,960 method. 47 00:03:07,960 --> 00:03:10,380 That's course, that values. 48 00:03:12,310 --> 00:03:15,372 And yes, that returned the list of every value, Ashley, 49 00:03:15,372 --> 00:03:17,631 Introducing Dictionaries and Beginner. 50 00:03:17,631 --> 00:03:21,641 Both of these methods returned just plain list that can be iterated over using 51 00:03:21,641 --> 00:03:22,264 a for loop. 52 00:03:22,264 --> 00:03:25,153 Now, if for some reason we needed this list sorted, 53 00:03:25,153 --> 00:03:28,971 we could wrap them in a call to the built in python function, sorted. 54 00:03:28,971 --> 00:03:32,921 Which will return a copy of the past list back in lexicographical order, 55 00:03:32,921 --> 00:03:36,561 which in this case, just means it will be sorted alphabetically. 56 00:03:36,561 --> 00:03:38,057 Let's clear this and take a look. 57 00:03:47,452 --> 00:03:50,069 That returned a list of the alphabetized keys. 58 00:03:50,069 --> 00:03:51,934 And then to do it again for values. 59 00:03:59,259 --> 00:04:00,491 There we go. 60 00:04:00,491 --> 00:04:03,278 Okay, now that we covered accessing keys and values, 61 00:04:03,278 --> 00:04:07,230 join me in the next video to learn about updating and mutating dictionaries.