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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:57:45+00:00 2026-06-04T21:57:45+00:00

In a Python program I’m writing, I’ve built up a linked list using a

  • 0

In a Python program I’m writing, I’ve built up a linked list using a dictionary which maps each node to its successor (with the last node mapped to None).

(Actually, the dictionary holds what Wikipedia tells me is called a spaghetti stack, which is a tree where each node is linked to its parent but not its children. This means there are many partially-overlapping paths from the leaf nodes to the root node. I only care about one of those paths, starting from a specific leaf node. This doesn’t really matter for the question, other than to rule out any solution that involves iterating over all the elements in the dictionary.)

I need to pass this list to another function as an iterable. I know I can do this using a generator function (see code below), but it seems like there ought to be a built-in function to make the iterator I need in one line (or a perhaps a generator expression). I’ve done a bit of searching through the documentation, but there’s nothing in the itertools or functools modules seems applicable, and I’m not sure where else to look.

Here’s the generator function I have now. The outer function can be eliminated (inlined) but the inner generator seems to be the only simple way to make an iterable for the data:

def makeListGenerator(nextDict, start):
    def gen(node):
        while node:
            yield node
            node = nextDict[node]
    return gen(start)

It seems like there should be a pattern for this sort of generator, but I’m not sure what it would be called. Here’s a generic version:

def makeGenericGenerator(nextFunc, continueFunc, start):
    def gen(value):
         while continueFunc(value):
             yield value
             value = nextFunc(value)
    return gen(start)

I could implement the specific version using this one using the call:

makeGenericGenerator(lambda v: nextDict[v], bool, start)

Does something like that already exist in the Python standard library?

  • 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-04T21:57:46+00:00Added an answer on June 4, 2026 at 9:57 pm

    The essential problem you face is that every time another value is taken from your iterable, your iterable has to remember that value, so that it knows how to generate the next value. In other words, your iterable needs to maintain its own state.

    That means that, whether or not there’s a good answer to your question, using a generator is probably the right solution — because that’s exactly what generators were made for! The whole point of generators is that they save their state between calls to their next method, and that’s exactly what you need.

    Generator expressions, on the other hand, are better for stateless transformations. A lot of people try to shoehorn state into them, and it’s generally kind of ugly. I actually spent a little while trying to do it for your case, and couldn’t get a generator expression to work. I finally found something that sort of works. This uses the little-known callable_iterator version of iter:

    >>> d = {1:2, 2:3, 3:4, 4:5, 5:None}
    >>> list(iter(lambda st=[1]: st.__setitem__(0, d[st[0]]) or st[0], None))
    [2, 3, 4, 5]
    

    I present this not as an answer, but as a demonstration of why this doesn’t work very well.

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

Sidebar

Related Questions

I have a small Python program, which uses a Google Maps API secret key.
I am writing a python program that lauches a subprocess (using Popen). I am
Writing a python program, and I came up with this error while using the
In a Python program I'm writing I've compared using a for loop and increment
I'm writing python program to build mac-address cache using pcap. But pcap module for
I'm writing a Python program with a GUI built with the Tkinter module. I'm
I'm writing a python program in which I need to overload the >> operator.
I've written a Python program which I distribute using pyinstaller. I've been using the
In the python program I'm writing, I've got a thread which iterates over a
The Python program I'm writing needs to start a local PHP script outside of

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.