Skip to content

Instantly share code, notes, and snippets.

@flopezluis
Created February 22, 2012 00:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save flopezluis/1880114 to your computer and use it in GitHub Desktop.
Save flopezluis/1880114 to your computer and use it in GitHub Desktop.
get the last element of a generator (python)
#modification of http://stackoverflow.com/questions/5983265/pythonic-way-of-determining-if-the-current-element-is-the-first-or-last-element
def annotate_last_item(gen):
prev_val = gen.next()
for val in gen:
yield False, prev_val
prev_val = val
yield True, prev_val
for last_item, obj in annotate_last_item(generator):
if last_item:
#yup
else:
#whatever
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment