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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:17:44+00:00 2026-05-18T00:17:44+00:00

I have a database that models a foldering relationship to n levels of nesting.

  • 0

I have a database that models a foldering relationship to n levels of nesting. For any given folder, I want to generate a list of all child folders.

Assuming I have a function called getChildFolders(), what is the most efficient way to call this kind of recursive loop?

The following code works for 4 levels of nesting, but I’d like more flexibility in either specifying the depth of recursion, or in intelligently stopping the loop when there are no more children to follow.

folder_ids = []
folder_ids.append(folder.id)
for entry in child_folders:
    folder_ids.append(entry.id)
    child_folders_1 = getChildFolders(entry.id)
    for entry_1 in child_folders_1:
        folder_ids.append(entry_1.id)
        child_folders_2 = getChildFolders(entry_1.id)
        for entry_2 in child_folders_2:
            folder_ids.append(entry_2.id)
            child_folders_3 = getChildFolders(entry_2.id)
            for entry_3 in child_folders_3:
                folder_ids.append(entry_3.id)
  • 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-18T00:17:45+00:00Added an answer on May 18, 2026 at 12:17 am

    A recursive function is a nice way to do this:

    def collect_folders(start, depth=-1)
        """ negative depths means unlimited recursion """
        folder_ids = []
    
        # recursive function that collects all the ids in `acc`
        def recurse(current, depth):
            folder_ids.append(current.id)
            if depth != 0:
                for folder in getChildFolders(current.id):
                    # recursive call for each subfolder
                    recurse(folder, depth-1)
    
        recurse(start, depth) # starts the recursion
        return folder_ids
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database, financials that has models created in django. In addition, it
I have a EDM (phoneDB) that models a back-end MSSQL database. I've developed a
I have a list of information sources in a database that I need to
I have a legacy database that I've set up some models to use. The
I have a pre-defined database that I want to map using Entity Framework 4
I have several fields in some of my database tables that my CakePHP models
I have a basic database that I want to pull out sub data (related)
I have a blog application that models a database using Entity Framework. The problem
I have two separate sets of tables in the same database that model the
I have category model that has a tree structure. In my database 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.