How to Modify Values in a Dictionary While Iterating Through It
00:19 Let’s move into the terminal, and I’m going to define a little dictionary here as an example for us. And the idea is just going to be that it’s going to be some fruits, and then some prices in cents, or fractions of a dollar. And, you know, you can imagine that if you were someone who had a store, you might want to keep track of your fruits and their prices in this way.
So, that’ll be a little simpler and a little easier to understand. Starting back up,
prices. Great. Let’s imagine, for example, you have your store and the economy’s not doing so great, maybe, or maybe it’s doing really well and you can charge 10 more cents for each fruit. So, how would you do that? Well, you might start out this way, and you’d be totally correct.
.1. So we’re increasing every price by 10 cents. And what does this do if we look at
prices? Well, it works pretty much perfectly, aside from this odd little floating-point addition madness that sometimes happens in Python. This is due to how floating-point numbers are stored in Python.
01:38 Sometimes they can be a little bit finicky. But either way, it’s clear that what we’ve done is we’ve added 10 cents to each of our original prices. So this is great, and this is really the paradigm you should use when you want to modify values in a dictionary at any time.
You’re pretty much always going to have to use the key paradigm and then access the original dictionary at that key. Now, you might be thinking “Well, we learned all of these cool ways of going through dictionaries and looking at things in a kind of easier manner.” You know, we can assign names to the key and the value, and then we can print them out and do stuff with them. So you might say, “Well, why don’t I just do
for key, value in prices.items():
value = value + .1.” Or let’s say
+ .2, just so it’s more obvious what happens.
But this doesn’t actually quite work the way that you might hope. Let’s take a look at what
prices is. Oh, it’s exactly the same as the last time we looked at it. Well, that’s kind of weird, because we went through and we iterated through.
print(key, value), then we get all of these items. So we clearly have access to them in some sense. But we can’t modify them in quite the same way. Well, why is this? Really, it comes back to this question about what is, actually,
And if we look at it, it’s a
dict_items object. And what this really means is it’s a specific object designed to show you what’s in a dictionary without actually giving you access to this dictionary.
03:09 And that’s really helpful when you’re designing large applications, because if you’re working with other programmers and other software packages, then you can give other people view access to what’s in the dictionary. They can look at your data, but they can’t modify it.
03:32 You really need to access the original dictionary using a key. Otherwise, you won’t really be able to change things. You’ll just be able to look at them, maybe even copy them, but you won’t be able to change them.
03:43 So, our next step is how do you modify keys in a dictionary? Well, one very, very simple way if you just want to add one, is you can just say, you know, your a dictionary at a new key equals some value.
And maybe you have a list you want to iterate through, and you can plug that in a list. But there are some circumstances where you might feel like what you want to do is say something like this,
for key in prices: and then you might want to start adding and subtracting from the dictionary as you go along with different keys, right?
Boom. We get a
RuntimeError. And I think the best way to explain why exactly this happens is to think about it in an analogy with a real person or set of people, right? Imagine someone gave you a bucket of fruits and they said “I want you to put a sticker on every fruit in this bucket.” But then as you were doing that, going through these fruits, they started to throw more fruits in the bucket and take some of them out before you were able to put stickers on them. That doesn’t work, right?
It doesn’t make any sense. What does it even mean to say
for key in prices when
prices is constantly changing? So, this doesn’t work, and this is something that you should avoid doing when you’re working with loops in Python—adding and deleting keys as you loop through.
If you really need to do it, though, what you can do is you can say
for key in list(prices.keys()):. So you can convert them to a
list, and then you can say
prices at, I don’t know—you can loop and simultaneously add and subtract from the dictionary however you want to do it. Right? So, let’s print out our
But you might think of other more complex applications where a similar principle happens. However, one thing to keep in mind is that converting it to a list does use more memory, because this
prices.keys(), it gets the keys one at a time from the dictionary, whereas converting it to a list makes you access all of these and put them in a data structure.
06:10 So if you’re doing this with a huge amount of data, it might not be ideal. So, we’ve gone over how to modify the values in a dictionary and how to modify the keys in a dictionary, and we’ve covered also some common pitfalls of ways that that might go wrong.
06:24 The key insight here is when you want to modify the values, you’re going to have to necessarily interact with the original dictionary. If you don’t, then you’re not going to actually get any results.
So we’d do something like this, and then we’d get
prices, and they’ve all been changed to
1, right? And then if you want to modify keys in a dictionary, it’s always safest to just do this kind of semi-manually, right? Let’s see.
'tomato', that’s a fruit, equals
06:56 It’s always safer to modify the keys in this way than it is to try to do them in a loop. And if you do need to do them in a loop and you need to change the keys as you go along, then convert it to a list first.
Become a Member to join the conversation.