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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:06:53+00:00 2026-06-07T18:06:53+00:00

How can I make a Python equivalent of pdtolist from Pop-11? Assume I have

  • 0

How can I make a Python equivalent of pdtolist from Pop-11?

Assume I have a generator called g that returns (say) integers one at a time. I’d like to construct a list a that grows automatically as I ask for values beyond the current end of the list. For example:

print a # => [ 0, 1, 2, g]
print a[0] # => 0
print a[1] # => 1
print a[2] # => 2
# (obvious enough up to here)

print a[6] # => 6
print a # => [ 0, 1, 2, 3, 4, 5, 6, g]
# list has automatically expanded

a = a[4:] # discard some previous values
print a # => [ 4, 5, 6, g]
print a[0] # => 4

Terminology – to anticipate a likely misunderstanding: a list is a “dynamic array” but that’s not what I mean; I’d like a “dynamic list” in a more abstract sense.

To explain the motivation better, suppose you have 999999999 items to process. Trying to fit all those into memory (in a normal list) all at once would be a challenge. A generator solves that part of the problem by presenting them one at a time; each one created on demand or read individually from disk. But suppose during processing you want to refer to some recent values, not just the current one? You could remember the last (say) ten values in a separate list. But a dynamic list is better, as it remembers them automatically.

  • 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-07T18:06:55+00:00Added an answer on June 7, 2026 at 6:06 pm

    Many thanks to all who contributed ideas! Here’s what I have gathered together from all the responses. This retains most functionality from the normal list class, adding additional behaviours where necessary to meet additional requirements.

    class DynamicList(list):
        def __init__(self, gen):
            self.gen = gen
    
        def __getitem__(self, index):
            while index >= len(self):
                self.append(next(self.gen))
            return super(DynamicList, self).__getitem__(index)
    
        def __getslice__(self, start, stop):
            # treat request for "last" item as "most recently fetched"
            if stop == 2147483647: stop = len(self)
            while stop >  len(self):
                self.append(next(self.gen))
            return super(DynamicList, self).__getslice__(start, stop)
    
        def __iter__(self):
            return self
    
        def next(self):
            n = next(self.gen)
            self.append(n)
            return n
    
    a = DynamicList(iter(xrange(10)))
    

    Previously generated values can be accessed individually as items or slices. The recorded history expands as necessary if the requested item(s) are beyond the current end of the list. The entire recorded history can be accessed all at once, using print a, or assigned to a normal list using b = a[:]. A slice of the recorded history can be deleted using del a[0:4]. You can iterate over the whole list using for, deleting as you go, or whenever it suits. Should you reach the end of the generated values, StopIteration is raised.

    Some awkwardness remains. Assignments like a = a[0:4] successfully truncate the history, but the resulting list no longer auto-expands. Instead use del a[0:4] to retain the automatic growth properties. Also, I’m not completely happy with having to recognise a magic value, 2147483647, representing the most recent item.

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

Sidebar

Related Questions

I want to make a program in Python that can take video input from
Possible Duplicate: How can I make an EXE file from a Python program? i
I have a Python script and I was wondering how I can make it
So I understand that mechanize can make python script behave exactly like a browser.
How i can make some improvement in my IRC client made in Python. The
How can one make recursive Glob() in a VariantDir() environment in Python? The answer
How can I make a keep alive HTTP request using Python's urllib2?
How can make a link within a facebox window that redirects it to another
I can make a variable's name from two variables' value: $a = 'tea'; $b
I can make a log in for easily, so that's not the problem. What

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.