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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:59:16+00:00 2026-06-01T13:59:16+00:00

How to get from this dict: cats = [ {‘parent_id’: False, ‘id’: 1, ‘title’:

  • 0

How to get from this dict:

    cats = [
    {'parent_id': False, 'id': 1, 'title': u'All'},
    {'parent_id': False, 'id': 2, 'title': u'Toys'},
    {'parent_id': 2, 'id': 3, 'title': u'Toypads'},
    {'parent_id': 3, 'id': 4, 'title': u'Green'},
    ]

Something like this?

cats = [
{'parent_id': False, 'id': 1, 'title': u'All'},
{'parent_id': False,
 'children': [{'parent_id': 2,
               'children': [{'parent_id': 3, 'id': 4,
                             'title': u'Green'}],
               'id': 3, 'title': u'Toypads'},
              [{'parent_id': 3, 'id': 4, 'title': u'Green'}]],
 'id': 2, 'title': u'Toys'}
]

I need it to build a menu\sub-menu in Jinja2.
I wrote a very bad code. It would be a more elegant solution.

    q = dict(zip([i['id'] for i in cats], cats))

    from collections import defaultdict
    parent_map = defaultdict(list)

    for item in q.itervalues():
        parent_map[item['parent_id']].append(item['id'])

    def tree_level(parent):
        for item in parent_map[parent]:
            yield q[item]
            sub_items = list(tree_level(item))
            if sub_items:
                for ca in cats:
                    if ca['id'] == item:
                        cats[cats.index(ca)]['children'] = sub_items
                        for s_i in sub_items:
                            try:
                                for ca_del_child in cats:
                                    if ca_del_child['id'] == s_i['id']:
                                        del cats[cats.index(ca_del_child)]
                            except:
                                pass
                yield sub_items
    for i in list(tree_level(False)):
        pass
  • 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-01T13:59:17+00:00Added an answer on June 1, 2026 at 1:59 pm

    Here is a fairly concise solution:

    cats = [{'parent_id': False, 'id': 1, 'title': u'All'},
            {'parent_id': False, 'id': 2, 'title': u'Toys'},
            {'parent_id': 2, 'id': 3, 'title': u'Toypads'},
            {'parent_id': 3, 'id': 4, 'title': u'Green'},]
    
    cats_dict = dict((cat['id'], cat) for cat in cats)
    
    for cat in cats:
        if cat['parent_id'] != False:
            parent = cats_dict[cat['parent_id']]
            parent.setdefault('children', []).append(cat)
    
    cats = [cat for cat in cats if cat['parent_id'] == False]
    

    Note that the comparisons to False are generally not necessary, but they should be used here in case you had a cat with 0 as its id or parent_id. In this case I would use None instead of False for a cat with no parent.

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

Sidebar

Related Questions

Basically the question is how to get from this: foo_id foo_name 1 A 1
I try to get string from this url: http://autoc.finance.yahoo.com/autoc?query=google&callback=YAHOO.Finance.SymbolSuggest.ssCallback When I look at this
Hopefully the last NN question you'll get from me this weekend, but here goes
how can i get [user_id] from this array? Array ( [2] => cbpaidSubscription Object
I need to get UserCarId(int) from this query and assign to int type variable.
Supposedly, it is possible to get this from Google Maps or some such service.
Anyone else get this or suffer from this? I am using Vista and VS
Hello this is code snippet which i get from Jquery Ajax based search I
I want to get the last result returned from this query. How do I
I am trying to scrape http://www.nscb.gov.ph/ggi/database.asp , specifically all the tables you get from

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.