Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Update and Mutate Dictionaries3:37 with Ashley Boucher
Add and change values in Python dictionaries.
Welcome back. 0:00 The topic of discussion for this video is updating and mutating dictionaries. 0:01 Dictionaries are mutable, that means we can change them in place. 0:05 And these changes are done using syntax we've already covered. 0:08 Let's keep working with our previous example down in the console. 0:12 The course dictionary here has three key value pairs. 0:16 To change the value for any of these keys, we access it using the key. 0:19 And then, like we assign a value to a variable, 0:22 we assign a new value to the key. 0:25 So say I want to change the value for the teacher key. 0:27 First, I'll access the value. 0:30 Then I assign it a new value using the equals sign. 0:35 Now this code will override what's in the dictionary. 0:42 If we inspect the contents of the dictionary now, 0:45 we'll see that Ashley is no longer the value associated with the teacher key. 0:47 The new value is Treasure. 0:53 Take a moment to try this on your own. 0:55 After opening the attached workspace, pause the video here and 0:57 try to change the value of the level key from beginner to intermediate. 1:00 When you're done, unpause the video and I'll show you the solution. 1:04 Welcome back, were you successful? 1:08 To change the value of the level key, 1:11 we access it using course [ 'level' ]. 1:15 Then we assign it a new value. 1:20 If we inspect the course dictionary now, 1:27 we could see the new value has overwritten the previous one. 1:29 Great, now adding an entirely new key value pair to the dictionary 1:34 uses the exact same syntax. 1:39 Let's say I wanna add another piece of data stages to the dictionary, 1:41 that's the number of stages in the course. 1:45 Just like when updating a keys value, I use course then square blankets and 1:47 then the name of the new key, in this case stages. 1:52 And then I assign it the value I want it to have. 1:58 This works because if Python can't find a matching key to update, 2:03 it will simply create a new key value pair inside the dictionary. 2:06 So let's check out the course dictionary now. 2:10 We can see our new key value pair stages with the value of 2 inside. 2:14 Give this a try yourself, positivity of here. 2:19 In the attached workspace, add a new key value pair to the provided dictionary. 2:22 The key and value can be whatever you like. 2:26 But make sure you test that it worked by saving and running your program, or 2:28 by copying and pasting the dictionary into the terminal and 2:32 working directly from there. 2:35 When you're done, unpause the video and we'll keep going. 2:37 If you have a question, please feel free to reach out in the Treehouse community. 2:39 All right, let's keep going. 2:43 Now we're gonna talk about removing key value pairs from a dictionary. 2:45 This is done using the built-in Python function del for delete. 2:49 Del accepts one argument, the value that you want removed. 2:52 So say we no longer wanted the stages key value pair we 2:56 added just a few moments ago. 2:59 To delete it, we call the del function, and then pass the value as an argument. 3:01 Let's inspect course again. 3:15 Great, the stages key value pair is gone. 3:19 Congratulations, you've now completed the first stage of introducing dictionaries, 3:23 celebrate your accomplishment. 3:27 In the next stage, we'll continue our discussion by learning about how to 3:29 iterate over dictionaries, and packing and unpacking. 3:31 See you soon, and keep on coding. 3:35
You need to sign up for Treehouse in order to download course files.Sign up