Explore how to use a for loop on a dictionary in Python with Art, a Python teacher at Noble Desktop. Discover how to create and iterate through dictionaries, and how to modify and manipulate them to create new dictionaries.
Key Insights
- Art demonstrates how to create a dictionary in Python, using the example of a 'menu' with food items and their prices.
- He shows how to use a for loop to iterate through the dictionary, which initially returns only the keys, or food items in this case.
- To fetch the corresponding values, or prices, of the keys, the key must be applied to the dictionary.
- Art proceeds to create a new dictionary called 'sale', which applies a 10 percent discount to each menu item price, demonstrating how to manipulate and modify original dictionaries.
- Another way to iterate through a dictionary is using the 'items' method, which converts it into a list of tuples. Each tuple consists of a key-value pair from the dictionary.
- Art finishes by creating another new dictionary, 'super sale', which applies a 20 percent discount to each menu item price, further showcasing the manipulation and modification of dictionaries in Python.
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.