As others have mentioned features added like this need careful examination not only at effects but costs.
As I see it, several somewhat different ideas were raised and one of them strikes me oddly. The whole point of an iterable is to AVOID calculating the next item till needed. Otherwise, you can just make something like a list. To talk about random access to an iterable is a tad weird as it would mean you need to get the first N items and store them and return the Nth item value as well as maintain the remainder of the unused part of the iterable. Further requests already in the cache would be gotten from there and any beyond it would require iterating more and adding more to the cache. So say my iterator returns the first N primes or just those below 100. What should be the functionality if you request item 1,000? As for reversing it, that requires you to basically do list(iterable) and use it up. What if the iterable is infinite as in all the odd numbers? If you really want an iterable that return something like prime numbers below some level in reverse order, that can be done by changing the iterable to create them going downward and that would be a different iterator. But how easily could you make some iterators go backward? Fibonacci, maybe so. Other things perhaps not. But again, as noted, anything already in a list can be set up as an iterator that returns one item at a time from that list, including in reverse. There won't be much savings as the data structure inside would likely be spread out to take all the memory needed, albeit it may simplify the code to look like it was being delivered just in time. As with many things in python, rather than asking for a global solution that affects many others, sometimes in unexpected ways, it may be more reasonable to make your own patches to your code and use them in ways you can control. In the case being discussed, you simply need to create a generator function that accepts an iterator, converts it to a list in entirety, reverses the list (or deals with it from the end) and enters a loop where it yields one value at a time till done. This should work with all kinds of iterators and return what looks like an iterator without any changes to the language. Of course, I am likely to be missing something. And, certainly, there may already be modules doing things like the above or the opportunity for someone to create a module such as the itertools module with nifty little functions including factory functions. -----Original Message----- From: Python-list <python-list-bounces+avi.e.gross=gmail....@python.org> On Behalf Of Oscar Benjamin Sent: Wednesday, May 3, 2023 3:47 PM To: python-list@python.org Subject: Re: An "adapter", superset of an iterator On Wed, 3 May 2023 at 18:52, Thomas Passin <li...@tompassin.net> wrote: > > On 5/3/2023 5:45 AM, fedor tryfanau wrote: > > I've been using python as a tool to solve competitive programming problems > > for a while now and I've noticed a feature, python would benefit from > > having. > > Consider "reversed(enumerate(a))". This is a perfectly readable code, > > except it's wrong in the current version of python. That's because > > enumerate returns an iterator, but reversed can take only a sequence type. > > Depending on what you want to give and receive, enumerate(reversed(a)) > will do the job here. Otherwise list() or tuple() can achieve some of > the same things. I don't think that is equivalent to the intended behaviour: reversed(enumerate(a)) # zip(reversed(range(len(a))), reversed(a)) enumerate(reversed(a)) # zip(range(len(a)), reversed(a)) In principle for a sequence input enumerate(a) could be something that behaves like a sequence and therefore could be reiterated or reversed etc. The enumerate(a).__reversed__ method could then delegate to a.__reversed__ and a.__len__ if they exist. This could be confusing though because the possible behaviour of enumerate(a) would be different depending on the type of a. -- Oscar -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list