Python Forum
Picking A number of items from a dict and getting there info - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Picking A number of items from a dict and getting there info (/thread-7605.html)



Picking A number of items from a dict and getting there info - TFish - Jan-17-2018

What i want to do it take a certain number of foods and take them and there values and print the values out.


foods = {"Dempsters Whole Wheat Bread": {"cost":2.27, "calories": 1620, "Servings":18},
         "Dempsters White Bread": {"cost":2.27, "calories": 1620, "Servings":18},
         "Dempsters Everything Bagels": {"cost":2.98, "calories": 1200, "Servings":12},
         "Gala Apple": {"cost":0.71, "calories": 52, "Servings":1},
         "Clemintines": {"cost":2.97, "calories": 700, "Servings":20},
         "Great Value Egg": {"cost":2.27, "calories": 2080, "Servings":6},
         "Nelson Milk": {"cost":4.27, "calories": 1620, "Servings":16},
         "Great Value Cheddar Cheese": {"cost":4.97, "calories": 1800, "Servings":5},
         "Great Value Vanilla Yougurt": {"cost":1.77, "calories": 600, "Servings":7},
         "Great Value Orange Juice": {"cost":1, "calories": 440, "Servings":4}}



RE: Picking A number of items from a dict and getting there info - wavic - Jan-17-2018

Do you have any question?

Do you get any errors or the output is not correct? Show us your code, the errors ( properly - BBcode ).

We are willing to help but we will not do it for you.


RE: Picking A number of items from a dict and getting there info - Larz60+ - Jan-17-2018

to access a food item, with food name as key:
food_item = foods['food name']
to access individual values in the selected item:
(used python 3.6 f-string
print(f'food_item: {food_item['cost']}')
To access all food items:
for item in foods.items():
    ...