Python Forum

Full Version: last pass of for x in iterator:
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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.
Code?
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.
(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?
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
(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".
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
(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.
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.
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
Pages: 1 2