Python Fundamentals Python Fundamentals

Adding, Removing, & Accessing Keys or Values

Check out a free preview of the full Python Fundamentals course:
The "Adding, Removing, & Accessing Keys or Values" Lesson is part of the full, Python Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Nina demonstrates how to add items, remove items, and access either the keys or values independently from each other using built-in functions.

Get Unlimited Access Now

Transcript from the "Adding, Removing, & Accessing Keys or Values" Lesson

[00:00:00]
>> Nina Zakharenko: Adding and removing from dictionaries. Dictionaries are themselves mutable.
>> Nina Zakharenko: So to add to our dictionary,
>> Nina Zakharenko: We put the new key in square brackets.
>> Nina Zakharenko: And then we use the assignment operation.
>> Nina Zakharenko: I'll give you a little hint. What happens if we try to
>> Nina Zakharenko: Do the same thing with,

[00:00:50]
>> Nina Zakharenko: With a key that's already there. Remember that with sets, we couldn't have duplicate items? Do you think we can have duplicate items, duplicate keys in our dictionary. We can't, right? Because of hash, yeah. And so if I do this, it just gonna overwrite the value that was there,

[00:01:20]
>> Nina Zakharenko: And discard the old one.
>> Nina Zakharenko: Just like with sets and lists, we can quickly see if an item is in the dictionary with the in keyword. You can also call .update, just like we did with a set. See that kind of sets and dictionaries are a little bit similar, right?

[00:01:53] Kind of think of a set like a, excuse me, you can kind of think of the keys in a dictionary like a set, right? There's no duplicates, or the duplicates get overwritten. It's really easy to see if an item is in a dictionary or not by its key.

[00:02:12] So, let's say we had two dictionaries. Let's say the key is kind of the letter that the color starts with.
>> Nina Zakharenko: And then I had a numbers dictionary here.
>> Nina Zakharenko: So colors, numbers. If I wanted to combine these two dictionaries together, I can call .update. And if I take a look at my colors now, we'll see that, the key-value pairs are combined.

[00:02:59] Just like with lists, there's nothing stopping you from mixing the types of the keys. And if we update from a dictionary that already has a particular key, what do you think will happen?
>> [INAUDIBLE].
>> Nina Zakharenko: Yeah. Yep, one incredibly useful scenario for dictionaries is storing the values in a list, right?

[00:03:27] We already know that mutable types can be the dictionary value. So let's say I have a dictionary of a color, and the value is a list of all the vegetables that are that color.
>> Nina Zakharenko: I can have as many items in here as I want, and just kind of going with one example.

[00:03:51] Actually, I'm going to rename this vegetables, okay. Now I can say, colors, pass in the key of green. What's the type of what this returns?
>> Student: List.
>> Nina Zakharenko: It's a list. What can I do with a list? Can easily add or remove items to it, right? So I'm just, I know well, let me test my assumption.

[00:04:23] So I think that vegetables with a key of green is gonna be a list. So let me call type on that, right? So I'm just verifying that, so because it's a list I can call an append on it. Now if I look at my colors, we will see that green, the key green maps to a value that's a list.

[00:04:46] And it has spinach in there, apples, etc. So, and this is kind of a really common pattern of storing a list of items associated to one key. Now there's three important methods that you need to remember when working with dictionaries, because dictionaries are key-value pairs. So going back to my dictionary of nums and let me update to be something less silly.

[00:05:26] And then okay, save my dictionary of numbers, calling .keys on this will get me back all of the keys in the dictionary. Calling .values will get all of the values.
>> Nina Zakharenko: What we're usually interested in, not always, which is why these methods are available. But usually when we're looking at a dictionary, we wanna look at the key-value pairs together.

[00:06:00] Those pairs, the key-value pair, that's called an item. So, we know that .keys gives me back all the keys, .values gives me back all the values. If the key value pair is an item, what method might we wanna call to get back those key value pairs?
>> Student: Items.

[00:06:21]
>> Nina Zakharenko: Gold star, nums.items. And that will return a list of tuples with the key-value pairs. And we'll see why this is a little bit interesting later, because remember what we can do with a tuple. We can easily unpack the values, right.