Accessing Keys and Values4:07 with Ashley Boucher
Discover how to access data from a Python dictionary
Lexicographical ordering is very similar to alphabetical ordering, but it considers additional characters besides the letters of the alphabet.
Some of the basic rules in Python are that:
1) Uppercase letters come earlier than lowercase letters. This means that A < Z < a < z.
2) Numbers come earlier than letters. This means that 0 < 9 < A < a.
3) Space characters come before all printable characters.
In this video, we're gonna talk about how to access keys and 0:00 values from inside a Python dictionary. 0:03 Do you remember how I mentioned previously that unlike sequences, 0:05 which are indexed by numbers, dictionaries are indexed by their keys? 0:09 We can use that difference to transfer what we know about accessing values and 0:13 sequences, to accessing values and dictionaries. 0:17 You see here in my workspace, I have a simple Python list. 0:19 Without any context, the data stored in this list doesn't make a lot of sense, 0:22 which is why soon, we're gonna convert it to a dictionary. 0:26 But, for right now, let's refresh how we get values out of this list. 0:29 If I want the first element in the list, I'll use the code course, 0:33 then square brackets, and then the index of the value I want to grab, 0:38 in this case, let's say zero. 0:43 The same syntax is used when pulling values from dictionaries. 0:45 Except instead of putting a numerical index inside the square brackets, 0:48 we put the values key. 0:52 So first let's back up and convert this list to a dictionary. 0:54 To do that, I'll first change the square brackets here to curly braces. 0:57 Then for each value in this I'll add a key, which is kinda like a label 1:04 Now to access that first value again, 1:24 I'll just change this code here to reflect the correct index. 1:26 And now I'll wrap that in a print statement, and then I'll save and 1:32 run the file to show the output. 1:35 Let's save. 1:40 Yep, it printed out Ashley. 1:51 So that the basics of accessing an individual value from a dictionary. 1:53 It's very similar to accessing an individual value from a sequence. 1:57 It's important to know that if you attempt to access a value with a non existing key, 2:01 Python won't just skip over it, you will get an error. 2:05 Now, Python also has ways to grab a list of all the keys or 2:08 all the values from a dictionary. 2:11 And even a built-in function to sort those lists into lexicographical order. 2:14 You might have learned about lexicographical ordering before, but 2:18 check the teacher's notes for more info. 2:21 So let's move down to the terminal where we can work a little more efficiently with 2:22 our dictionary. 2:26 I'm gonna start the interpreter and copy the dictionary down there. 2:26 So, first, to get a list of all the keys in the dictionary, 2:38 I'll use the keys method. 2:41 To use that I type the name of the dictionary which is course then dot and 2:43 then keys, the name of the method, and then parens, 2:47 just like a regular function call. 2:50 When I hit Enter, it will show the return value from the keys method. 2:52 Yep, here's a list of every key, teacher, title and level. 2:58 Similarly, we can access a list of every value in the dictionary using the values 3:02 method. 3:07 That's course, that values. 3:07 And yes, that returned the list of every value, Ashley, 3:12 Introducing Dictionaries and Beginner. 3:15 Both of these methods returned just plain list that can be iterated over using 3:17 a for loop. 3:21 Now, if for some reason we needed this list sorted, 3:22 we could wrap them in a call to the built in python function, sorted. 3:25 Which will return a copy of the past list back in lexicographical order, 3:28 which in this case, just means it will be sorted alphabetically. 3:32 Let's clear this and take a look. 3:36 That returned a list of the alphabetized keys. 3:47 And then to do it again for values. 3:50 There we go. 3:59 Okay, now that we covered accessing keys and values, 4:00 join me in the next video to learn about updating and mutating dictionaries. 4:03
You need to sign up for Treehouse in order to download course files.Sign up