Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Still working with dicts
#1
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])
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#2
you might like this: https://python-forum.io/Thread-Create-Dy...dictionary
Reply
#3
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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Star Recursively convert nested dicts to dict subclass Alfalfa 1 2,904 Jan-22-2021, 05:43 AM
Last Post: buran
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,383 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  convert List of Dicts into a 2 deep Nested Dict rethink 1 3,212 Aug-23-2019, 05:28 PM
Last Post: ichabod801
  Merge dicts without override chisox721 4 3,249 Jul-20-2019, 01:45 AM
Last Post: chisox721
  Python inventory system with dicts. 2skywalkers 7 5,951 Aug-23-2018, 05:06 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020