Using a For Loop for Dictionaries in Python

Free Video Tutorial and Guide

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.

Python for Data Science Bootcamp: Live & Hands-on, In NYC or Online, Learn From Experts, Free Retake, Small Class Sizes,  1-on-1 Bonus Training. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

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.

photo of Noble Desktop

Noble Desktop

At Noble Desktop you can learn how to code websites, build iOS apps, make graphics, and more. From our front-end coding bootcamp to Photoshop classes, we offer a variety of comprehensive day, evening, and weekend training classes as well as certificate programs in web design, web development, HTML email, and more. Courses are available in-person (at our training facility in Soho), live online, and customized corporate or private training. Our course materials and workbooks are used by colleges and schools worldwide. Learn a skill today and start using it tomorrow.

More articles by Noble Desktop

How to Learn Python

Master Python with hands-on training. Python is a popular object-oriented programming language used for data science, machine learning, and web development. 

Yelp Facebook LinkedIn YouTube Twitter Instagram