On Wed, Oct 7, 2020 at 9:06 AM Josh Rosenberg <shadowranger+pythonid...@gmail.com> wrote: > > This: > > def advance(it, n): > try: > return it[n:] > except TypeError: > return itertools.islice(it, n, None) > > has the disadvantages of: > > 1. Requiring a temporary copy of the data sliced (if len(it) is 1_000_000, > and n is 500_000, you're stuck between 500_000 pointless __next__ calls, a > 500_000 long temporary list or inefficiently looking up 500_000 elements by > index) > 2. Not working with sequence iterators, only sequences themselves (so if you > want to read 1000, skip 1000, read 1000, skip 1000, over and over, you can't > just use a single stateful iterator for the whole process without using the > consume recipe and calling __next__ 1000 times for each skip) > 3. Not working with all sorts of things where a dedicated advance > implementation would allow efficient skipping, e.g. the new insertion ordered > dicts when no deletions have been performed (a state we already track for > other reasons) can advance the iterator (for the raw dict, keys, values or > items) with the same efficiency as sequences do (and could save a lot of work > building and tossing tuples iterating .items() even if it can't assume no > dummy entries); the various itertools functions like combinations, > permutations and combinations_with_replacement (and in some cases, product) > could also be advanced efficiently without the expense of generating the > intermediate states. > > Point is, the OP's case (a single sequence, advanced exactly once) is the > only one that implementation addresses, and it still has scaling issues even > then. >
You trimmed off the part where I actually made a proposal :) As you may notice from the name of the parameter, this is intended to actually work with iterators, not sequences. ChrisA _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/SYAK5BZZWVFLY4XDRRZTTJEWON3WIR7Q/ Code of Conduct: http://python.org/psf/codeofconduct/