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

  • Home
  • SEARCH
  • 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 3481740
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:28:50+00:00 2026-05-18T10:28:50+00:00

I have a structure that looks like this: [ {‘id’: 4, ‘children’: None}, {‘id’:

  • 0

I have a structure that looks like this:

[ {'id': 4, 'children': None},
  {'id': 2, 'children': 
    [ {'id': 1, 'children':
        [ {'id': 6, 'children': None},
          {'id': 5, 'children': None} ]
      },
      {'id': 7, 'children':
        [ {'id': 3, 'children': None} ]
      }
    ]
  }
]

I also have a list of selected IDs, [4, 5, 6, 7]. I want to traverse the list and for each object in the list, add a selected key with a value of 1 if it is selected, and 0 if it is not.

Currently I am doing this recursively with this function:

def mark_selected(tree, selected):
    for obj in tree:
        obj['selected'] = 1 if obj['id'] in selected else 0
        if obj['children'] is not None:
            obj['children'] = mark_selected(obj['children'], selected)
    return tree

This seems to work fine, but I was wondering if there was a more clever way to do this, possibly using list comprehension or generators.

Can anyone come up with a more elegant solution for 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-05-18T10:28:51+00:00Added an answer on May 18, 2026 at 10:28 am

    Recursion is perfectly elegant. List comprehensions don’t apply as you’re altering the structure in place, rather than producing a new sequence. As for generators, you could write a DFS or BFS traverser.

    def dfs(nodes):
        if nodes is not None:
            for node in nodes:
                yield node
                yield from dfs(node['children'])
    
    for node in dfs(tree):
        node['selected'] = node['id'] in selected
    

    Python 3.3 and later can use the recursive yield above (yield from syntax). Earlier versions would instead loop over the recursive results, yielding those:

    def dfs(nodes):
        if nodes is not None:
            for node in nodes:
                yield node
                for child in dfs(node['children']):
                    yield child
    

    If the list of IDs to select is large, it would be more performant to convert it from a list to a set, which will speed up lookup (the node['id'] in selected).

    selected = set(selected)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data structure that looks like this: public class Node { public
I have a DOM structure that looks like this: <div id=comment-1234 class=comment>...</div> <div id=comment-2391
I have a data structure that looks like this (simplified version) class A(models.Model): a
I have a data structure that looks like Array ( [0] => Array (
I have data in my database that looks like this: Date (DateTime) Type (varchar)
I have an HTML Table that looks like this: The user can update any
I have some nicely-structured data that looks like this: CREATE TABLE SourceBodyPartColors ( person_ID
I have a jsp page that is trying to reference some user defined classes.
I'm attempting to design a system that will allow the processing of multiple types

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.