Skip to main content
Noble Desktop/3 min read

Using a For Loop for Dictionaries in Python

Dictionary Loop Patterns

for key in dict

Iterates over keys (default behavior).

for value in dict.values()

Iterates over values only.

for key, value in dict.items()

Iterates over both — most common pattern.

Sorted Iteration

for k in sorted(dict): if you need keys in order.

Master Python at Noble Desktop

Noble Desktop's Python Programming Immersive covers AI APIs, data analysis, and modern Python development.

In this video, we're going to look at how to use a For Loop for Dictionaries in Python

Video Transcription

Hi, my name is Art and I teach Python at Noble Desktop. In this video, I'm going to show you how to use a for loop on a dictionary to iterate through it.

Let's create a variable called 'menu' and assign it a dictionary. Suppose we have a hamburger with a price of 5.75, nachos at 9.99, and a salad at 2.75.

Now, how would you iterate through the dictionary? So you could use a for loop and let's say 'k' in menu. Let's start right here and print that key and the K would be hamburger, nachos, and salad.

So you see when you apply a for loop to a dictionary, it will get the keys. But what about the values? Now, it's really easy if you know the key, you could fetch the value.

Right, so how would you fetch the value? You need to apply that key to the dictionary, so it's going to be 'menu' and then we could pass the key, and then you see we get all the values.

Let's create a new dictionary called 'sale', and that's an empty dictionary. We could use dictionary 'sale' and assign the 'round' to that. So each item from the dictionary 'menu' would be used as a key in the new dictionary 'sale' and the value you see, we decrease that value by 10 percent and we're rounding down it to two digits after the decimal point.

Now if you run it, you see that would be a brand new dictionary. That would be the easiest way to iterate through a dictionary. However, there is another option that you could iterate through a dictionary. So to do that, you need to take a dictionary which is 'menu' and then you need to use the method 'items'. So the method 'items' will convert it to dictionary items, but frankly this is a list of tuples.

Now since that's a list of tuples, we could iterate through that. We could do 'for' and let's use the same variable name 'key' in 'menu items'. Let's print and now you see each item represents a tuple. Please notice that each tuple got exactly two values.

Since every tuple here got two values, we could unpack it with two variable names. So let's call this 'key' so that's going to be our variable name, and 'value', right? Now since we got two, 'hamburger' would be assigned to 'key' and 5.75 to 'value'.

If you want to do the same stuff again that we just did here, so you could create a super 'sale' dictionary and suppose for super 'sale' we want to reduce prices by 20 percent. How are we going to do it? We could use and populate that super 'sale' and use a key and then the value, the value would be reduced by 0.2 and we could use the same 'round'.

Now if you run 'super sale', you see we're getting this new dictionary populated with the same items where prices reduced by 0.2. It's not reduced, it's actually gone up. Of course, 0.8, so now it makes sense. Now you're paying not 9.99 for nachos, but you're paying 7.99 for nachos. Thank you and watch my other Python videos.