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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:20:23+00:00 2026-05-26T01:20:23+00:00

class Node(object): def __init__(self, lst): if type(lst) == list: self.value = lst[0] self.children =

  • 0
class Node(object):
    def __init__(self, lst):
        if type(lst) == list:
            self.value = lst[0]
            self.children = lst[1:]
        else:
            self.value = lst
            self.children = []
    @property
    def ChildElements(self):
        return [Node(a) for a in self.children]

    @property
    def GetValue(self):
        return self.value

def node_recurse_generator(node):
    yield node.value
    for n in node.ChildElements:
        node_recurse_generator(n)

Node is a simple tree like data structure. The list’s first element is always the value of the Node, further elements will be children. If the Node is initiated with something other than a list, that value will be that, and children will be [], an empty list.

a = Node([1,[10,20,30],[100,200,300]])
>>> list(node_recurse_generator(a))
[1]

Now, it would be great to have a recursive iterator over all the elements, but my generator only yields the first element. Why is this happening?

  • 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-26T01:20:23+00:00Added an answer on May 26, 2026 at 1:20 am

    Simply calling node_recurse_generator recursively isn’t enough – you have to yield its results:

    def node_recurse_generator(node):
        yield node.value
        for n in node.ChildElements:
            for rn in node_recurse_generator(n):
                yield rn
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code in django.template: class Template(object): def __init__(self, template_string, origin=None, name='<Unknown
I wrote following code: class node: def __init__(self, title, series, parent): self.series = series
I have list of objects type Node ( items ). And Node class has
I have a base class Node which contains a list of child nodes. Node
I have a Node class that has an 'element' slot which contains a list
payment-tags.py @register.simple_tag def has_purchased(user_id): payments = Payment.objects.all(user__id=user_id) return PaymentObjects(payments) class PaymentObjects(template.Node): def __init__(self, payments):
template <class T> void BT<T>::inOrder(void (*inOrderPtr)(T&)) { inOrderPtr(inOrder(this->root)); } template <class T> void BT<T>::inOrder(Node<T>*
I'm writing a generic template tag that could return a model's field value dynamically
I want to extend the Nokohiri::XML::Node object into my own unique object with custom
I tried adding a custom method to the Node class but ended up creating

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.