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.
00:10
As you can see, we’ve got our trusty old reliable example
dictionary, which maps 'color'
to 'red'
, 'fruit'
to 'apple'
, and 'species'
to 'dog'
.
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.
00:30
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 'greeting'
, obviously.
00:48
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.
01:07
What happens? Ah! color
, fruit
, and species
. So, first off, we did in fact get something. And we got what? The keys!
01:16
'color'
, 'fruit'
, and 'species'
. So you can just iterate directly through a dictionary if you want to access the keys, which is pretty amazing.
01:25
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 key
.
01:39
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.
01:56
Just to emphasize that. So we see that color
maps to red
, fruit
maps to apple
, 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.
02:18
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.
02:27
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 '__iter__'
.
02:39
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.
02:55
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.
03:13
We can say for value in example.values():
03:19
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:33
But there’s also a .keys()
, 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.
03:54
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.
04:08
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.
04:28
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.
04:52
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.
05:12
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? example.items():
print(key)
.
05:24
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 .items()
thing.
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.