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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:07:18+00:00 2026-06-10T20:07:18+00:00

I am stuck with the following data. There is a list. [{name: ‘/’, children:

  • 0

I am stuck with the following data.

There is a list.

[{name: '/', children: [{name: 'bin'}, {name: 'sbin'}, {name: 'home'}]},
{name: 'home', children: [{name: 'user1'}, {name: 'user2'}]},
{name: 'user2', children: [{name: 'desktop'}]}]

I want to convert above list to the following dictionary.

{name: '/', children: [{name: '/bin'}, {name: '/sbin'}, {name: '/home', children: [{name: 'user1'}, {name: 'user2', children: [{name: 'desktop'}]}]}]}

I write some codes to convert data above style.

def recT(data, child, parent, collector):
    dparent = dict(name=parent)
    dchildren = dict()
    lst = []
    for c in child:
        lst.append(dict(name=c['name']))
        for d in data:
            if c['name'] == d['name']:
                if len(d) > 1:
                    dchildren.update(dict(children=recT(data, d['children'], d['name'], collector)))
    dparent.update(dchildren)
    collector.update(dparent)
    return lst

Then,

myd = dict()
for d in data2:
    if len(d) > 1:
        recT(data2, d['children'], d['name'], myd)

NOTE: data2 is dictionary list I want to covert.

But, the output dictionary is the last record in list:

{'children': [{'name': 'desktop'}], 'name': 'user2'}

Please help.

  • 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-10T20:07:20+00:00Added an answer on June 10, 2026 at 8:07 pm

    As lazyr said, you can’t duplicate keys in your dict like that. You could convert it to a format like the following to be valid python dict syntax:

    {
      '/': {
        'bin': {}, 
        'sbin': {}, 
        'home': {
          'user1': {},
          'user2': {
            'desktop': {}
          }
      }
    }
    

    The reason you’re only getting the last record in the list is because your dict uses unique keys

    mydict = {}
    mydict['name'] = 1
    mydict['name'] # is 1
    mydict['name'] = 2
    
    for x,y in mydict.iteritems():
      print '{0}: {1}'.format(x,y)
    >> name: 2 # Note only one entry
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

there's something I can't get my head round... Basically I'm given the following data
I have some files, consisting of end of day stock data in the following
I'm stuck on following matter: I have a datagrid with 10 items... I also
I am trying to use Hibernate+Spring in my application but stuck into following error:
I'm stuck with the following. I'm programming a game and I need to get
I'm stuck with the following scenario and appreciate any help/advice.. Requirement I have number
I'm new to wicket and stuck with the following problem: I have a table
I`m new to sql and have been stuck on the following issue for almost
im learning linked lists in java but am stuck on the following Before deleting
I'm a beginner programmer and I'm stuck with the following problem: If I have

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.