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
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
Skaperen and micseydel 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
Gribouillis and DeaD_EyE like this post
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to pass a dictionary as an argument inside setup function of unittest nilaybnrj 1 86 May-11-2019, 03:18 AM
Last Post: keames
  iterator from an iterator Skaperen 3 150 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 152 Apr-08-2019, 10:05 AM
Last Post: avinash
  PyODBC error - second parameter to executemany must be a sequence, iterator, or gener RBeck22 1 203 Mar-29-2019, 06:44 PM
Last Post: RBeck22
  How to pass a variable to subprocess.check_call champweller 0 117 Mar-15-2019, 02:50 AM
Last Post: champweller
  Pass variable script return twice output problem Faruk 8 310 Dec-26-2018, 11:57 AM
Last Post: Faruk
  how to program robot to pass wise man puzzle steven12341234 0 231 Dec-02-2018, 08:31 AM
Last Post: steven12341234
  The derivate class dosn't behave like the base when i pass parameter to them drudox 4 409 Aug-05-2018, 06:42 PM
Last Post: drudox
  Windows - Get Handle to Pass as Parameter malonn 1 347 Jul-27-2018, 01:54 PM
Last Post: malonn
  Cannot open file in an OpenSharedItem() iterator - The file may not exist/may be open S3RG388 3 948 Jul-25-2018, 10:18 AM
Last Post: gontajones

Forum Jump:


Users browsing this thread: 1 Guest(s)