How to match key with another dictionary to retrieve a value

I’m having trouble retrieving the values from “order_menu” when the key match’s with the same key in a different dictionary - “order_dictionary”.

order_menu = {("C", "Chips (Scoops)") : 2.80, ("F", "Fish (Battered)") : 2.90, ("Fc", "Fish (Crumbed)") : 4.90, ("Fib", "Filet (Battered)") : 6.90, ("Fic", "Filet (Crumbed)") : 6.90, 
     ("Hd", "Hot Dog") : 2.60, ("S", "Sausage") : 2.60, ("Mp" , "Meat Patty (Homemade)") : 3.90, ("Cr", "Crabstick") : 2.50, ("Sr", "Spring Roll (Homemade)") : 2.80, 
     ("Cr", "Curry Roll (Homemade)"): 2.80, ("Pof", "Potato Fritter (Homemade)") : 1.20, ("Paf", "Paua Fritter (Homemade)") : 5.90, ("Cn", "Chicken Nugget") : 1, 
     ("Mh", "Mini Hot Dog (On a stick)") : 1.20, ("Pf", "Pineapple Fritter") : 2.50}
order_dictionary = {"C" : 3, "Hd" : 5, "Cn" : 2}

for food, quantity in order_dictionary.items():
    if len(order_dictionary) > 0:
        print("• {} x{} (${})".format(food, quantity))
    else:
        print("You have not orded anything yet!")

Inside the loop the food variable will change to each individual key of order_dictionary you are iterating over. Just use this key and the [] operator to access the correspondig dicitionary item’s value: order_menu[food]

Additional advices:

You are looping over order_dictionary. It means that the loop body will never execute when order_dictionary is empty so the condition len(order_dictionary) > 0 is redundant.

Also in Python it is preferred to write just: if order_dictionary instead of if len(order_dictionary) > 0. Both have the same meaning.

I was not attentive enough. Later I noticed that you do not use the the food abbreviations like "C" as the dictionary keys, you use tuples of the abbreviation and the full name like ("C", "Chips (Scoops)"):

In your case this does not make much sense as this prevents you utilizing the main advantage of dictionaries when you want to access the items by their abbreviations. Use just the abbreviation as the key if you want to find the item by the abbreviation in your code:

order_menu = {"C": ("Chips (Scoops)", 2.80),
    ...

Then you can access the items using the abbreviation:

food_abbreviation = "C"
food_name, food_price = order_menu[food_abbreviation]

Manish, (@GruffCheetah628).

I hope you are making good progress on your coding.

Please reply to your original topic when the subject is the same. This will keep the topic list tidy.

These are all the same topic:

  1. Checking dictionary keys against list items
  2. How to match key with another dictionary to retrieve a value
  3. Making value2 in a ((key1, key2) : (value1, value2)) dictionary iterable
  4. And this deleted topic: How do I access/call the second value in a ((key1, key2) : (value1, value2)) dictionary?