How to loop through lists and dictionaries

I have a task where I have to loop through a list and then dictionaries to display the total stock worth of a cafe. I created some code that was reviewed but was told these comments:

Try looping through the menu list.

Each item can be used as keys in the dictionaries, to retrieve matching stock and price values.

Stock worth is the sum of each stock item multiplied by its price.

Im just having some trouble on how I would convert the list items to keys and then add them to the dictionary.

My original code was as follows:

menu = ['cheeseburger', 'chicken nuggets', 'fish', 'chips']
total = 0
stock = {1: 25,
         2: 20,
         3: 18,
         4: 10

price = {1: 40,
         2: 35,
         3: 28,
         4: 18

for stock in price:
    total = total + price[stock]
total = float(total)
print("The total stock worth is R" + (str(total)))

Thank you for any help and advice!

Try this:

menu = ['cheeseburger', 'chicken nuggets', 'fish', 'chips']
stock = {'cheeseburger': 25,
         'chicken nuggets': 20,
         'fish': 18,
         'chips': 10

price = {'cheeseburger': 40,
         'chicken nuggets': 35,
         'fish': 28,
         'chips': 18

total = 0
for food in menu:
    total += stock[food] * price[food]


Python, While working with dictionary, we can have a case in which we need to iterate through the lists, which are in the keys of dictionaries. This kind� To loop or iterate over each element of a dictionary variable, you have to use the for loop of Python. You can get the dictionary variable keys and values in the output. Use the for loop of the Python and use only keys or values in your programming. In the dictionary variable, the keys are related to its relevant values.

Lets go point by point -

1. Try looping through the menu list.

---> Will do this. You will understand why this is needed.

2. Each item can be used as keys in the dictionaries, to retrieve matching stock and price values.

---> You should use the items present in menu list as keys in dictionaries you have created for stock and price instead of using numbers for keys.

This helps to visualize and understand the code and variable structure more clearly.

So you should change it to :-

stock = {'cheeseburger': 25, 'chicken nuggets': 20, 'fish': 18, 'chips': 10}

price = {'cheeseburger': 40, 'chicken nuggets': 35, 'fish': 28, 'chips': 18}

3. Stock worth is the sum of each stock item multiplied by its price.

---> Now you are calculating the total price of stocks by just adding prices of all the stocks. Comment states that the logic for calculating the total stock price should be sum of stock_units*price_of_one_stock for each item in menu.

For doing this you need to iterate through all the items of the menu. Calculate total stock price for each item in menu and add it to final total value.

Something like this:

total = 0
for item in menu:
    total = total + stock[item]*price[item]

How to Iterate Through a Dictionary in Python – Real Python, Dictionaries map keys to values and store them in an array or collection. The keys must be of a hashable type, which means that they must have a hash value that� If you do .Keys.Orderby() you'll iterate on a list of keys. If that's all you need, fine. If you need values, then in the loop you'd have to query the dictionary on each key to get the value. In many scenarios it won't make a practical difference. In high-performance scenario, it will.

What you have been asked to do is this

menu = ['cheeseburger', 'chicken nuggets', 'fish', 'chips']
stock = {'cheeseburger' : 25, 'chicken nuggets' : 20, 'fish' : 18, 'chips' : 10}
price = {'cheeseburger' : 40, 'chicken nuggets' : 35, 'fish' : 28, 'chips' : 18}
total = 0
for i in menu:
    total += stock[i] * price[i]

The value of total, after the for loop is 2384, which is the required answer

Iterate through list of dictionaries and get key and value. : Python, I want to be able to loop through all dictionaries of the list and extract both the key and its corresponding value. Any idea as to how I would be able to accomplish� If you use a list comprehension to iterate through the dictionary’s values, then you’ll get code that is more compact, fast, and Pythonic: >>> incomes = { 'apple' : 5600.00 , 'orange' : 3500.00 , 'banana' : 5000.00 } >>> total_income = sum ([ value for value in incomes . values ()]) >>> total_income 14100.0

Different ways to Iterate / Loop over a Dictionary in Python , It returns a iterable View object of all key,value elements in the dictionary. Its backed by original dictionary. Let's iterate over the list using� Taking the length of one list and using a placeholder index variable, often named i, you manually access each position in the lists. a = [1,2,3,4,5] b = [10,20,30,40,50] for i in range (len (a)):

How to iterate through a dictionary in Python?, There are two ways of iterating through a Python dictionary object. One is to fetch associated value for each key in keys() list. >>> D1 = {1:'a'� Python 3.5 iterate through a list of dictionaries. Ask Question and for each dictionary in the list, it will iterate over the keys and print its values.

How to iterate over a LIST of dictionaries? I know how to iterate over , items(): print(k,v) But I am struggling to figure out the way to iterate a list of dictionary. Please help. # Example: # values = [{� zip() is the best way to iterate over multiple arrays,list and json objects. Using zip(), we can iterate over given two json dictionary with a single for loop. Using zip(), we can iterate over given two json dictionary with a single for loop.

  • The comments are telling you that instead of using 1-4 as the keys for the dictionary, use cheeseburger-chips
  • Thank you for the advice! then why would I need to loop through the menu?
  • Because then you can just get the values directly out of the dictionary via stock[food] instead of having to use one dictionaries key as the accessor to the other
  • Thank you guys for the help and understanding!
  • Welcome @Rycan. Please mark answer as accepted if it answers your question. :)