Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Learn how to iterate over dictionaries using for loops.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
[MUSIC]
0:00
Welcome to stage two of
Introducing Dictionaries.
0:05
In the second half of this course, we'll
be discussing iteration, packing, and
0:07
unpacking.
0:11
Iterating over dictionaries uses a for
loop, just like iterating over sequences.
0:12
So continuing on with
our previous example,
0:17
here we have a simple
dictionary called course.
0:19
Let's write a basic loop to
iterate over the dictionary.
0:22
It should look very similar to a for
loop used to iterate over a sequence.
0:25
Let's save and run and
see what this prints out.
0:35
Okay, this may not have been
exactly what you expected.
0:44
As you can see,
only the keys were printed.
0:48
It didn't give us the key value pair or
0:50
anything regarding the values
in the dictionary at all.
0:53
To access the value for
each key as the loop progresses,
0:56
one option is to use
the syntax you learned for
0:59
accessing values earlier in this course,
that would look like this.
1:01
Let's save and run again.
1:16
Okay, that gives us each of the values,
but
1:22
there's a more Pythonic way to do this.
1:24
There is a Python method called items
that can be used on dictionaries.
1:27
And items returns a list of tuples,
where each tuple contains both the key and
1:31
the value for
every item in the dictionary.
1:35
And let's look at just that.
1:38
I'm gonna come back to my code editor and
comment this out for a second.
1:40
And we're gonna look at course.items.
1:47
Take a look at each
tuple inside this list.
2:01
The first element in each
tuple is the item's key, and
2:04
the second is the item's value.
2:08
So what this means is that we can use
the items method to loop over a list of
2:10
tuples representing
the dictionaries's keys and
2:15
value instead of iterating
directly over the dictionary.
2:19
So let's erase this print statement.
2:24
Uncomment our code here, and then I'm
gonna change course to course.items.
2:28
And I will tell this to print item again.
2:36
Now if we run this, it will print
out each tuple inside course.items.
2:39
But to take this a step further,
2:44
think back to what you might know about
unpacking tuples and multiple assignment.
2:46
Tuples can be unpacked
into different variables,
2:51
where each element of the tuple
is assigned to its own variable.
2:53
This is handy in loops.
2:57
If we change item in our for loop to
a tuple, like key, value, for instance,
2:59
we can extrapolate the data in each
tuple into its own variable and
3:04
use it within our loop.
3:09
Let's take a look at what this looks like.
3:11
Let's change a code here
to use multiple assignment.
3:13
Now, the item variable in this code
will always reference the current tuple
3:16
in the list that's returned
from course.items.
3:21
If we replace item with key, value,
which is indeed another tuple,
3:25
then we signal to the interpreter that
we want the first element in the current
3:30
tuple to be assigned to key and the second
element to be assigned to value.
3:36
Now we can access the key and
3:42
the value of the current item in
the dictionary separate from one another.
3:44
Let's try printing both of them and
then save and run.
3:49
Cool, now we have our keys and
our values accessible inside the for
4:03
loop in a clean Pythonic syntax.
4:07
Congrats, join me in the next video to
learn about packing with dictionaries.
4:09
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up