Skip to content

Instantly share code, notes, and snippets.

@sebastibe
Created September 9, 2012 00:09
Show Gist options
  • Save sebastibe/3681426 to your computer and use it in GitHub Desktop.
Save sebastibe/3681426 to your computer and use it in GitHub Desktop.
Finding the last item in a loop
def iter_islast(iterable):
""" iter_islast(iterable) -> generates (item, islast) pairs
Generates pairs where the first element is an item from the iterable
source and the second element is a boolean flag indicating if it is the
last item in the sequence.
"""
it = iter(iterable)
prev = it.next()
for item in it:
yield prev, False
prev = item
yield prev, True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment