Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8947971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:53:37+00:00 2026-06-15T12:53:37+00:00

I have a dictionary object as such: menu = {‘dinner’:{‘chicken’:’good’,’beef’:’average’,’vegetarian’:{‘tofu’:’good’,’salad’:{‘caeser’:’bad’,’italian’:’average’}},’pork’:’bad’}} I’m trying to create

  • 0

I have a dictionary object as such:

menu = {'dinner':{'chicken':'good','beef':'average','vegetarian':{'tofu':'good','salad':{'caeser':'bad','italian':'average'}},'pork':'bad'}}

I’m trying to create a graph (decision tree) using pydot with the ‘menu’ data this.

‘Dinner’ would be the top node and its values (chicken, beef, etc.) are below it. Referring to the link, the graph function takes two parameters; a source and a node.

It would look something like this:

Except ‘king’ would be ‘dinner’ and ‘lord’ would be ‘chicken’, ‘beef’, etc.

My question is: How do I access a key in a value? To create a tree from this data I feel like I need to create a loop which checks if there is a value for a specific key and plots it. I’m not sure how to call values for any dictionary object (if it’s not necessarily called ‘dinner’ or have as many elements.).

Any suggestions on how to graph it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T12:53:38+00:00Added an answer on June 15, 2026 at 12:53 pm

    Using a recursive function

    You might want to consider using a recursive function (like the visit in my code below), so that you are able to process a general nested dictionary. In this function, you want to pass a parent parameter to keep track of who is your incoming node. Also note you use isinstance to check if the dictionary value of a key is a dictionary of its own, in that case you need to call your visit recursively.

    import pydot
    
    menu = {'dinner':
                {'chicken':'good',
                 'beef':'average',
                 'vegetarian':{
                       'tofu':'good',
                       'salad':{
                                'caeser':'bad',
                                'italian':'average'}
                       },
                 'pork':'bad'}
            }
    
    def draw(parent_name, child_name):
        edge = pydot.Edge(parent_name, child_name)
        graph.add_edge(edge)
        
    def visit(node, parent=None):
        for k,v in node.iteritems():# If using python3, use node.items() instead of node.iteritems()
            if isinstance(v, dict):
                # We start with the root node whose parent is None
                # we don't want to graph the None node
                if parent:
                    draw(parent, k)
                visit(v, k)
            else:
                draw(parent, k)
                # drawing the label using a distinct name
                draw(k, k+'_'+v)
    
    graph = pydot.Dot(graph_type='graph')
    visit(menu)
    graph.write_png('example1_graph.png')
    

    Resulting tree structure

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have such dictionary Dictionary<string, object> , dictionary holds string keys and objects as
I have a Dictionary object that is formed using a double as its key.
I have a dictionary object which i would like to encrypt, then put it
Possible Duplicate: Modifying .NET Dictionary while Enumerating through it I have a dictionary object
in runtime i will have a dynamic dictionary object return to me e.g var
I have an object Dictionary<int, Dictionary<int, Foo> > on one side, and a function
I have a string data which I need to parse into a dictionary object.
I have a simple Dictionary(of String, Object) that I need to iterate through and
I have an object allStudents = Dictionary<ClassRoom, List<Student>>() In Linq how would I get
I have the following nested dictionaries: Dictionary<int, Dictionary<string, object>> x; Dictionary<int, SortedDictionary<long, Dictionary<string, object>>>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.