How to Iterate Through Dictionaries Using Basic Python Tools
00:00 Hi everyone! In this video, we’re going to be going through how to iterate through dictionaries using basic Python tools. So, let’s get right into it and head over to the terminal.
As you can see, we’ve got our trusty old reliable
example dictionary, which maps
00:18 So, we already talked about how you can look at just one element of a dictionary. You can plug in a key and you get out the value, right? So given a key, you can access a value.
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.
01:03 A lot of things in Python work exactly this way. So let’s give it a shot.
What happens? Ah!
species. So, first off, we did in fact get something. And we got what? The keys!
'species'. So you can just iterate directly through a dictionary if you want to access the keys, which is pretty amazing.
And let’s see how we can get the values with just the keys. Well, we can now say
for key in example: print(key), and then we can just say
example at the
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.
We can just plug in an empty dictionary and we can then call the
dir() function on it to see just what its attributes are.
And the reason I’m interested in this is because among all of these awesome methods and things that we see here—some of which we’ll look at later, like
'popitem'—there is one called
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.
We can say
for value in example.values():
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.
But there’s also a
for key in example.keys():, which does exactly the same thing. This works just like iterating straight through the dictionary.
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.
But what this really is great for is you can use tuple unpacking and you can say
for key, value in example:—not
ellipsis, what’s that?
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.