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 9022331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:28:54+00:00 2026-06-16T05:28:54+00:00

I have a python dict that looks like this {‘data’: [{‘data’: [{‘data’: ‘gen1’, ‘name’:

  • 0

I have a python dict that looks like this

{'data': [{'data': [{'data': 'gen1', 'name': 'objectID'},
                   {'data': 'familyX', 'name': 'family'}],
          'name': 'An-instance-of-A'},
         {'data': [{'data': 'gen2', 'name': 'objectID'},
                   {'data': 'familyY', 'name': 'family'},
                   {'data': [{'data': [{'data': '21',
                                        'name': 'objectID'},
                                       {'data': 'name-for-21',
                                        'name': 'name'},
                                       {'data': 'no-name', 'name': None}],
                              'name': 'An-instance-of-X:'},
                             {'data': [{'data': '22',
                                        'name': 'objectID'}],
                              'name': 'An-instance-of-X:'}],
                    'name': 'List-of-2-X-elements:'}],
          'name': 'An-instance-of-A'}],
'name': 'main'}

The structure is repeating and its rule is like:

  • A dict contains ‘name’ and ‘data’
  • ‘data’ can contain a list of dicts
  • If ‘data’ is not a list, it is a value I need.
  • ‘name’ is a just a name

The problem is that for each value, I need to know every info for each parent.

So at the end, I need to print a list with items that looks something like:

objectID=gen2 family=familyY An-instance-of-X_objectID=21 An-instance-of-X_name=name-for-21

Edit: This is only one of several lines I want as the output. I need one line like this for each item that doesn’t have a dict as ‘data’.

So, for each data that is not a dict, traverse up, find info and print it..

I don’t know every function in modules like itertools and collections. But is there something in there I can use? What is this called (when I am trying to do research on my own)?

I can find many “flatten dict” methods, but not like this, not when I have ‘data’, ‘name’ like this..

  • 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-16T05:28:55+00:00Added an answer on June 16, 2026 at 5:28 am

    This is a wonderful example what recursion is good for:

    input_ = {'data': [{'data': [{'data': 'gen1', 'name': 'objectID'},
                       {'data': 'familyX', 'name': 'family'}],
              'name': 'An-instance-of-A'},
             {'data': [{'data': 'gen2', 'name': 'objectID'},
                       {'data': 'familyY', 'name': 'family'},
                       {'data': [{'data': [{'data': '21',
                                            'name': 'objectID'},
                                           {'data': 'name-for-21',
                                            'name': 'name'},
                                           {'data': 'no-name', 'name': None}],
                                  'name': 'An-instance-of-X:'},
                                 {'data': [{'data': '22',
                                            'name': 'objectID'}],
                                  'name': 'An-instance-of-X:'}],
                        'name': 'List-of-2-X-elements:'}],
              'name': 'An-instance-of-A'}],
    'name': 'main'}
    
    def parse_dict(d, predecessors, output):
        """Recurse into dict and fill list of path-value-pairs"""
        data = d["data"]
        name = d["name"]
        name = name.strip(":") if type(name) is str else name
        if type(data) is list:
            for d_ in data:
                parse_dict(d_, predecessors + [name], output)
        else:
            output.append(("_".join(map(str,predecessors+[name])), data))
    
    result = []
    
    parse_dict(input_, [], result)
    
    print "\n".join(map(lambda x: "%s=%s"%(x[0],x[1]),result))
    

    Output:

    main_An-instance-of-A_objectID=gen1
    main_An-instance-of-A_family=familyX
    main_An-instance-of-A_objectID=gen2
    main_An-instance-of-A_family=familyY
    main_An-instance-of-A_List-of-2-X-elements_An-instance-of-X_objectID=21
    main_An-instance-of-A_List-of-2-X-elements_An-instance-of-X_name=name-for-21
    main_An-instance-of-A_List-of-2-X-elements_An-instance-of-X_None=no-name
    main_An-instance-of-A_List-of-2-X-elements_An-instance-of-X_objectID=22
    

    I hope I understood your requirements correctly. If you don’t want to join the paths into strings, you can keep the list of predecessors instead.

    Greetings,

    Thorsten

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

Sidebar

Related Questions

I have a Python 2.x module.py file that looks like this: class A(object): KEYWORD
I have a dict of configuration variables that looks something like this: self.config =
I have a python dict that looks like: defaultdict(<type 'int'>, {u'RT': 1, u'be': 1,
I have a lot of archive data in python dict format that I am
Say I have a string that looks like this: str = The &yquick &cbrown
I have a python dict like this: my_dict = {'find': ['http://time.com', 'http://find.com'], 'time': ['http://any.com',
I have a string that looks like this: Name1=Value1;Name2=Value2;Name3=Value3 Is there a built-in class/function
I have a custom file format for graphs which looks like this: node1.link1 :
I have a dictionary that looks like that: grades = { 'alex' : 11,
So I have a python dict like: 1:[ red,blue,green] 2: [blue,blue,red].. and so on.

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.