Skip to content

Instantly share code, notes, and snippets.

@tebeka
Created September 8, 2011 00:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tebeka/1202260 to your computer and use it in GitHub Desktop.
Save tebeka/1202260 to your computer and use it in GitHub Desktop.
from itertools import chain
def istail(it):
'''Check if iterator has one more element. Return True/False and
iterator.'''
try:
i = next(it)
except StopIteration:
return False, it
try:
j = next(it)
return False, chain([i, j], it)
except StopIteration:
return True, chain([i], it)
t, it = istail(iter([]))
print t, list(it)
t, it = istail(iter([1]))
print t, list(it)
t, it = istail(iter([1, 2]))
print t, list(it)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment