Python Forum
last pass of for x in iterator: - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: last pass of for x in iterator: (/thread-18438.html)

Pages: 1 2


last pass of for x in iterator: - Skaperen - May-17-2019

i have (a reference to) an iterator that came from some function. i need to loop through the iterator. i need to "know" (something that indicates) when the last pass of the loop is happening. my first thought is to get the value len(iterator). but not all iterators implement __len__(). so, i am looking for some other way to have a flag set to a ttue value during the last pass of the loop.


RE: last pass of for x in iterator: - SheeppOSU - May-18-2019

Code?


RE: last pass of for x in iterator: - micseydel - May-18-2019

Python iterators do not support what you want. You could wrap it in something that caches look-ahead values and then you could query on that object, but I'll leave that as an exercise to the reader.


RE: last pass of for x in iterator: - Skaperen - May-18-2019

(May-18-2019, 12:14 AM)SheeppOSU Wrote: Code?

there isn't any code, yet.

(May-18-2019, 12:20 AM)micseydel Wrote: Python iterators do not support what you want. You could wrap it in something that caches look-ahead values and then you could query on that object, but I'll leave that as an exercise to the reader.

right. i already know that iterators don't support this. you mean like make a memory-hogging list that causes the process to get swapped in and out as the list is iterated while the body of the loop counts down from the size?


RE: last pass of for x in iterator: - michalmonday - May-18-2019

If by any chance you have to apply some additional action to the last iterated item then you could use else keyword.

for i in range(2):
    print(i)
else:
    print("Last item once more:", i)
Output:
0 1 Last item once more: 1

(May-18-2019, 12:14 AM)SheeppOSU Wrote: Code?

for i in my_iter:
# what he has to do here to accomplish what he clearly explained



RE: last pass of for x in iterator: - micseydel - May-18-2019

(May-18-2019, 12:45 AM)Skaperen Wrote: you mean like make a memory-hogging list that causes the process to get swapped in and out as the list is iterated while the body of the loop counts down from the size?
You only need to look ahead by one. I wouldn't automatically call that "memory-hogging".


RE: last pass of for x in iterator: - Yoriz - May-18-2019

def last_iter(iterable):
    try:
        previous_item = next(iterable)
    except StopIteration:
        return
    for current_item in iterable:
        yield previous_item, False
        previous_item = current_item
    yield previous_item, True


for item, is_last in last_iter(i for i in range(5)):
    print(item, is_last)
Output:
0 False 1 False 2 False 3 False 4 True



RE: last pass of for x in iterator: - Skaperen - May-18-2019

(May-18-2019, 01:54 AM)michalmonday Wrote: If by any chance you have to apply some additional action to the last iterated item ...
actually, i need to not do an action on the last item.

@micseydel i thought you meant to make a whole list. the one-behind generator from @Yoriz looks like the way to go. i assume, now, that's what you meant.


RE: last pass of for x in iterator: - Gribouillis - May-20-2019

Skaperen Wrote:actually, i need to not do an action on the last item.
For a nice API, you could improve itertools.islice() by allowing a negative stop argument.


RE: last pass of for x in iterator: - buran - May-20-2019

I know you are not big fan of installing extra packages but more-itertools has some interesting tools you may find useful in this case