If I am reducing over a collection in Python, what’s the most efficient way to get the rest of the collection (the unvisited items) ? It is quite often that I need to reduce over a collection, but I want my reducing function to take the unvisited items of the collection I am reducing over.
edit – to clarify, I want something like:
reduce(lambda to-return, item, rest: (code here), collection, initial)
where rest is the items not yet seen by my lambda
This is the best I can do. It expects that the “collection” be sliceable:
Note that this is very poorly tested. Please test thoroughly before you attempt to use this in any real code. If you really want this to work for any iterable, you could put a
collection = tuple(collection)in there at the top I suppose (assuming you have enough memory to store your entire iterable in memory at once)