Bottom Page

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.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#2
Code?
micseydel likes this post
Age Doesn't Matter, Your Passion Does
- Shreya Jain
Quote
#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.
Quote
#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?
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#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
Quote
#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".
Quote
#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
micseydel and Skaperen like this post
Quote
#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.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#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.
DeaD_EyE likes this post
Quote
#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
DeaD_EyE and Gribouillis like this post
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Pass Dictionary to Method is not possible Frank123456 1 72 Aug-19-2019, 10:18 AM
Last Post: buran
  looking for a sprcil iterator Skaperen 7 260 Jun-13-2019, 01:40 AM
Last Post: Clunk_Head
  Pass command line argument with quotes koticphreak 3 336 May-31-2019, 11:18 PM
Last Post: ichabod801
  How to pass a variable to subprocess.check_call champweller 1 268 May-26-2019, 07:10 AM
Last Post: heiner55
  How to pass a dictionary as an argument inside setup function of unittest nilaybnrj 1 250 May-11-2019, 03:18 AM
Last Post: keames
  iterator from an iterator Skaperen 3 289 Apr-17-2019, 06:56 AM
Last Post: Skaperen
  unable to pass a input after changing the user from root to non root using python avinash 3 281 Apr-08-2019, 10:05 AM
Last Post: avinash
  PyODBC error - second parameter to executemany must be a sequence, iterator, or gener RBeck22 1 545 Mar-29-2019, 06:44 PM
Last Post: RBeck22
  Pass variable script return twice output problem Faruk 8 447 Dec-26-2018, 11:57 AM
Last Post: Faruk
  how to program robot to pass wise man puzzle steven12341234 0 304 Dec-02-2018, 08:31 AM
Last Post: steven12341234

Forum Jump:


Users browsing this thread: 1 Guest(s)