I would like to know if there is a class available either in the standard library or in pypi, which matches this description.
The constructor will take an iterator.
It will implement the container protocol (i.e. __getitem__, __len__, etc.), so that slices, length, etc. will be executed. At the same time, it will iterate over and save only enough values from the constructor argument to provide any information requested.
So, if jitlist [6] is requested, it will call self.source.next () 7 times, save these elements in its list and return the last one.
This will allow you to use the code down to use it as a list, but to avoid unnecessarily creating an instance of the list for cases where the list function is not needed, and to avoid allocating memory for the entire list if only a few members are requested.
It seems pretty easy to write, but also seems useful enough that someone would probably already make this available in the module.
source
share