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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:26:07+00:00 2026-06-02T20:26:07+00:00

I need to be able to take lists containing strings and nested lists of

  • 0

I need to be able to take lists containing strings and nested lists of strings like the following:

['parent', ['child', 'child2', ['grandchild', ['ggrandchild'], 'grandchild2'], 'child3'], '2parent', '3parent' ['3child', ['3grandchild']]]

And print strings for each parent, each parent-child, each parent-child-grandchild, etc.:

'parent'
'parent_child'
'parent_child2'
'parent_child2_grandchild'
'parent_child2_grandchild_ggrandchild'
'parent_child2_grandchild2'
'parent_child3"
'2parent'
...
etc

I have been able to get it to work to a depth of two nested levels with the following code:

def list_check(parsed_list):
    for item in parsed_list:

        if type(item) != list and prefix == []: 
            prefix.append(str(item))
            print item

        elif type(item) != list and prefix != []: 
            print prefix[0]+"-"+item

        elif type(item) == list:
            list_check(item)

        else:
            pass

But I’m struggling with getting it to work for arbitrary nesting depth. I’ve taken a basic approach of tracking the nesting depth through a stack, but my implementation is broken in an obvious way that I don’t know how to fix.

It currently deletes what is in the stack even if there are still other children following. What I want it to do is only pop items out of the relevant stacks if the sublist is ending.

prefix = []
nesting_depth = []

def list_check(parsed_list):
    for item in parsed_list:

        if type(item) == list:

            nesting_depth.append('1')
            list_check(item)

        elif type(item) != list and prefix == []: 
            prefix.append(str(item))
            print item

        elif type(item) != list and prefix != []: #this where i need another condition like 'and item is last in current sublist'
            print prefix[0:len(nesting_depth)]+"-"+item
            prefix.pop()

        else:
            pass

How do I reference something like ‘if it’s the last item in the current sublist of parsed_list’ in a way that accommodates the recursion of the overall function?

  • 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-02T20:26:08+00:00Added an answer on June 2, 2026 at 8:26 pm

    A dictionary would be much better suited to your needs. But here’s what you can do with the data structure you have:

    def format_tree(tree):                                                                                            
        prefix = ''
        for node in tree:
            if isinstance(node, basestring):
                prefix = node
                yield node
            else:
                for elt in format_tree(node):
                    yield prefix + '_' + elt 
    

    A simple test:

    >>> a = ['parent', ['child', 'child2', ['grandchild', ['ggrandchild']], 'child3']]
    >>> print '\n'.join(format_tree(a))
    parent
    parent_child
    parent_child2
    parent_child2_grandchild
    parent_child2_grandchild_ggrandchild
    parent_child3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to be able to take EPS and PDF's and convert them to
I have a list of strings, I need to be able to simply probe
I need a Regex Statement (run in c#) that will take a string containing
Users of the website need to able to store images in their area ,
Need to be able to pull Magento products into an external template. Need to
I need to be able to disable the selection of future dates within the
I need to be able to scroll a RichTextBox to the bottom, even when
I need to be able to tell if a link (URL) points to an
I need to be able to run RegLoadKey() on a remote machine, and it
I need to be able to start/stop a per-session GUI agent from a root

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.