Python Forum
last pass of for x in iterator:
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
last pass of for x in iterator:
#1
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Code?
Reply
#3
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.
Reply
#4
(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?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
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
Reply
#6
(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".
Reply
#7
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
Reply
#8
(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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#9
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.
Reply
#10
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to pass encrypted pass to pyodbc script tester_V 0 800 Jul-27-2023, 12:40 AM
Last Post: tester_V
  prime numbers with iterator and generator cametan_001 8 1,771 Dec-17-2022, 02:41 PM
Last Post: cametan_001
  resetting an iterator to full Skaperen 7 6,808 Feb-20-2022, 11:11 PM
Last Post: Skaperen
  popping an iterator Skaperen 11 3,600 Oct-03-2021, 05:08 PM
Last Post: Skaperen
  q re glob.iglob iterator and close jimr 2 2,178 Aug-23-2021, 10:14 PM
Last Post: perfringo
  Problem with an iterator grimm1111 9 4,217 Feb-06-2021, 09:22 PM
Last Post: grimm1111
  Multi-class iterator Pedroski55 2 2,335 Jan-02-2021, 12:29 AM
Last Post: Pedroski55
  Pass by object reference when does it behave like pass by value or reference? mczarnek 2 2,513 Sep-07-2020, 08:02 AM
Last Post: perfringo
  is a str object a valid iterator? Skaperen 6 5,539 Jan-27-2020, 08:44 PM
Last Post: Skaperen
  discard one from an iterator Skaperen 1 1,957 Dec-29-2019, 11:02 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020