Created
February 22, 2012 00:17
-
-
Save flopezluis/1880114 to your computer and use it in GitHub Desktop.
get the last element of a generator (python)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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