Python Forum

Full Version: Still working with dicts
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Given the current dict and data structure, doing a search for chicken pulls all recipes for chicken. If I wanted to refine that search for bourbon chicken, how would I go about digging deeper to get that key? Or should the data storage be different. I've been looking into json a little.
Thanks
Current scenario
recipes = {
    "chicken":[
        {"bourbon":[
            {"ingedients":"burbon, chicken, stove, pan","directions":"cook it and eat it"}
            ]},
        {"fried":[
            {"ingredients":"grease, chicken, seasonings","directions":"fry chicken and put seasons on it then eat"}
            ]}
        ],
    "beef":[
        {"hamburger":[
            {"ingredients":"1 pound of ground beef, forman grill, salt and pepper","directions":"you know the drill"}
            ]},
        {"spagetti":[
            {"ingredients":"noodles, sauce, hamburger","directions":"cook the stuff and pig out"}
            ]

            }
        ]
    }



print(type(recipes))

x = 'chicken'
if x in recipes:
    print(recipes[x])
Some possibilities:

>>> for recipe in recipes['chicken']:
...     print(list(recipe)[0])
... 
bourbon
fried
>>> [list(recipe)[0] for recipe in recipes['chicken']]
['bourbon', 'fried']
>>> for recipe in recipes['chicken']:
...     if 'bourbon' in recipe:
...         print(recipe)
... 
{'bourbon': [{'ingedients': 'burbon, chicken, stove, pan', 'directions': 'cook it and eat it'}]}
>>> for recipe in recipes['chicken']:
...     if 'bourbon' in recipe:
...         print(recipe.get('bourbon'))
... 
[{'ingedients': 'burbon, chicken, stove, pan', 'directions': 'cook it and eat it'}]
>>> for recipe in recipes['chicken']:
...     if 'bourbon' in recipe:
...         print(recipe.get('bourbon')[0]['ingedients'])
... 
burbon, chicken, stove, pan