Check out a free preview of the full Python Fundamentals course

The "Looping Over Dictionaries" Lesson is part of the full, Python Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Nina demonstrates how to loop over dictionaries.

Preview
Close

Transcript from the "Looping Over Dictionaries" Lesson

[00:00:00]
>> Nina Zakharenko: If you want to loop over a dictionary, let's say I have a dictionary of hex colors. And red is #FF and then green is,
>> Nina Zakharenko: #008. So this is my dictionary. If I tried to loop over my dictionary just as is,
>> Nina Zakharenko: What am I gonna see here?

[00:00:41]
It's just the keys.
>> Nina Zakharenko: If I wanted to loop over the keys and values in my dictionary at once, which method do I have to use?
>> Speaker 2: .items?
>> Nina Zakharenko: .items, that's exactly right, so I would put .items here, and what does .items return, what's the data structure?
>> Speaker 3: Q value per,

[00:01:06]
>> Speaker 4: Tuples.
>> Nina Zakharenko: Tuples, yeah, it's a list of tuples, and what can we do with tuples? We can unpack them. So we can use that tuple unpacking in our for loop and we can say, for and give a variable name for the key.
>> Nina Zakharenko: And then give the variable name to the value,

[00:01:34]
>> Nina Zakharenko: And loop over that. The key here is to call .items on our dictionary so that we get that key-value pair all at once.
>> Nina Zakharenko: A lot better than 4i equals i++, kinda that sorta stuff, right?
>> Speaker 5: So in this case, you have to increment at the end, what if you wanted to increment at the beginning?

[00:02:02]
Like do i++, doing ++i, where you'd increment first before you started the comparison.
>> Nina Zakharenko: Increment from i. If you want to get fancy with the sequence, you can use range to generate the indices and then just manually use that index in your list.
>> Speaker 5: Normally people increment that way, maybe that's normally how people do it in the end.

[00:02:37]
>> Nina Zakharenko: Normally people increment, well, you wanna start at 0 and go from there. Right, yeah, a lot more readable. If on the off case that you need to get the index and the item at that index at the same time, there's a built in function called enumerate. So if I look at my list of colors, I could say, for i, that's the index, and the color in, and then call this built-in enumerate, if I can spell it right, and pass in my list of colors.

[00:03:22]
That's gonna return a list of tuples in enumerate colors, for the first item in the tuple is the position or the index and the second item is the item itself.
>> Speaker 2: You would want that to be i though, not index?
>> Nina Zakharenko: Yes, thank you.
>> Nina Zakharenko: Okay, so index 0, color red, index 1, color green, index 2, color blue, pretty straightforward.

[00:03:59]
So a common error when looping over a dictionary, I had my hex colors dictionary, what if I try to do for key value in hex colors?
>> Nina Zakharenko: What would happen here?
>> Nina Zakharenko: I'm not using dot items, so what am I gonna be looping over?
>> Speaker 2: The keys.
>> Nina Zakharenko: Just the keys, yeah.

[00:04:29]
So I'm gonna get a tuple unpacking error because I'm only getting a key back but I'm trying to assign it to a key value tuple. Remember, if we had a tuple and,
>> Nina Zakharenko: We unpacked it and we didn't provide the right number of arguments, we got the same error, too many values to unpack.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now