How to Iterate Through Dictionaries Using Basic Python Tools
But what if you want to access all of the values, or get to all of the keys? And if you don’t know the keys in advance, you obviously can’t say—you know, if you don’t know if there’s a key named
'greeting' or not, then you’re going to risk if you try to access it that there’s no key named
So, how do you iterate through the keys in a dictionary? Well, your first instinct, if you’re familiar with iterating through lists, might be to just say
for thing in example:
print(thing). And that’s a reasonable approach.
Boom. And oh great, look! If we go back to
example, we’ve got the keys and we’ve got the values, right? And we could get a little fancier, if we wanted to, say, print a nice little mapping marking just to emphasize this. Oh, sorry.
Just to emphasize that. So we see that
color maps to
fruit maps to
species maps to
dog, et cetera. So, that’s all super easy. Well, how does this actually work? Well, to check this out, we can use the
dir() function, which is a very nice builtin that Python gives you which lets you examine the inner functions and attributes of any object.
And if we do
dir() of a list, we’ll see it also has an
'__iter__'. And so anything in Python that has this built-in
.__iter__() function, you can just iterate through with a
for loop, or even other kinds of iterators.
You can just say
for key in example: print(key), as I already showed you. So, this is pretty sweet, but what you might say is “Well, this is cool and all, but maybe I don’t want to…For example, maybe I want to just access the values or something.” Luckily, Python has a way of dealing with this as well.
and then we’ll just say
print(value). Oh look! There we are. Just the values, right? This is a pretty cool set of functions that a dictionary provides and we could see it as well if we were to go back up to when you printed out all of the inner functions.
03:44 But you might be saying, “Well, why is this useful, then, if it’s just the same as going through the keys? And even the values, you can get all the values from the keys if you have the keys.” Well, let’s take a look again.
We can just print out, like,
example.values() and see exactly what it is. Well, it’s a
dict_values object. And I’ll tell you, you wouldn’t know this quite from looking at it, but this
dict_values object is a view object, which is a really interesting kind of object.
It’s a dynamic view on the values of the dictionary, so it changes as the dictionary does. But what it does for you that’s different than just iterating straight through the dictionary is that you can pass this
example.values() object to other applications without giving access to your dictionary, which is really useful when you start to get into object-oriented design.
And then one final way to get access to these keys and values is a very convenient one, which I like to use a lot because it’s really very simple. You can say
for item in example.items():
print(item). Aha! So, what do we have here? Well, here we have, again, these keys and values.
But what kind of arrangement are they in? Well, to see what that is we can say
print(type(item)). Ah, sorry. Here we are.
print(type(item)). Well, they’re all tuples. Which is pretty cool, and it’s a nice way of doing this because then you can have arbitrarily many items and they’re all represented in a nice way.
And then we’ll again use this nice little mapping thing. And you can get access to the key and the value with just one simple statement. And so this means you don’t have to rely on the original dictionary at all inside your
for loop, because you already have access to a representation of the key and the value, just as it is, with this
05:47 So, that’s some simple ways to iterate through dictionaries. In our next video, we’re going to cover how to actually modify those keys and values as you move through the dictionary. So stay tuned for that, and thanks for listening.
Become a Member to join the conversation.