Skip to content

Instantly share code, notes, and snippets.

@jsbueno

jsbueno/iter_check_last.py

Last active Oct 15, 2020
Embed
What would you like to do?
_sentinel = object()
def iter_check_last(iterable):
iterable = iter(iterable)
current_element = next(iterable, _sentinel)
while current_element is not _sentinel:
next_element = next(iterable, _sentinel)
yield (next_element is _sentinel, current_element)
current_element = next_element
@jsbueno

This comment has been minimized.

Copy link
Owner Author

@jsbueno jsbueno commented Oct 15, 2020

In [107]: for is_last, el in iter_check_last(range(3)):
     ...:     print(is_last, el)
     ...: 
     ...: 
False 0
False 1
True 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment