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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:48:25+00:00 2026-06-12T20:48:25+00:00

I’m having some trouble finding a way to get a list index against a

  • 0

I’m having some trouble finding a way to get a list index against a list of nested lists.

For example I can find out how many nodes, or the structure of the list for a given node with the following two functions.

t = ['add', [ \
        ['divide a', [ \
            ['if statement', ['Var 9', 'Var 5', 'Var 1', 'Var 4']], \
            ['subtract', [ \
                ['add', [ \
                    ['round to integer', ['Var 10']], 'Var 4'] \
                ], 'Var 9' \
            ]] \
        ]], 'Var 4' \
     ]]

def total_nodes(structure,num_nodes):
    s = structure
    print "N:" , num_nodes , s
    num_nodes += 1
    if isinstance(s,str): return num_nodes    
    if s[1]:
        for x in range(len(s[1])):
            num_nodes = total_nodes(s[1][x], num_nodes)
        return num_nodes 


def get_structure_for_node(structure,counter,find_node=1):
    s = structure
    if not isinstance(counter,int):
        return counter
    if counter == find_node:
        return s
    counter += 1
    if isinstance(s,str): return counter
    if s[1]:
        for x in range(len(s[1])):
            counter = get_structure_for_node(s[1][x],counter,find_node=find_node)
        return counter



print 
print total_nodes(t,0)
print
print get_structure_for_node(t,0,find_node=12)

OUTPUT:

N: 0 ['add', [['divide a', [['if statement', ['Var 9', 'Var 5', 'Var 1', 'Var 4']], ['subtract', [['add', [['round to integer', ['Var 10']], 'Var 4']], 'Var 9']]]], 'Var 4']]
N: 1 ['divide a', [['if statement', ['Var 9', 'Var 5', 'Var 1', 'Var 4']], ['subtract', [['add', [['round to integer', ['Var 10']], 'Var 4']], 'Var 9']]]]
N: 2 ['if statement', ['Var 9', 'Var 5', 'Var 1', 'Var 4']]
N: 3 Var 9
N: 4 Var 5
N: 5 Var 1
N: 6 Var 4
N: 7 ['subtract', [['add', [['round to integer', ['Var 10']], 'Var 4']], 'Var 9']]
N: 8 ['add', [['round to integer', ['Var 10']], 'Var 4']]
N: 9 ['round to integer', ['Var 10']]
N: 10 Var 10
N: 11 Var 4
N: 12 Var 9
N: 13 Var 4
14

Var 9

From the output I can see the path from t to the node ’12’ we searched for would be:

t[1][0][1][1][1][1]

but I have been unable to find a way to keep track of this index key as I am going through the recursive function. which I require to change elements of the list / tree

Any takers?

I should add that I had been trying to track it by adding in a variable to the recursion which builds a string of where it went i.e.

path = “10112101”

then trying to work with this later, however I’ve been unable to get it accurate and would prefer a cleaner way.

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

    The simplest flatting algorithm is:

    def flat_list(l):
        for node in l:
            if isinstance(node, list):
                for i in flat_list(node):
                    yield i
            else:
                yield node
    

    It’s easy to modify flatting algorithm to keep track:

    def flat_list_keeping_track(l, track=None):
        track = track or ()
        for i, node in enumerate(l):
            new_track = track + (i,)
            if isinstance(node, list):
                for result in flat_list_keeping_track(node, track=new_track):
                    yield result
            else:
                yield node, new_track
    

    And now you can type

    def get_structure_for_node(structure, find_node=1):
        return list(flat_list(structure))[find_node][1]
    

    This is not the fastest way. If your structure is big and you use relatively small ‘find_node’ values then you should write something like that (based on get-the-nth-item-of-a-generator-in-python):

    import itertools
    def get_structure_for_node(structure, find_node=1):
        return next(itertools.islice(flat_list(structure), find_node, find_node+1))[1]
    

    You can also modify other flatten function if the speed is really important (see making-a-flat-list-out-of-list-of-lists-in-python or making-a-flat-list-out-of-a-multi-type-nested-list).

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.