Skip to content

Instantly share code, notes, and snippets.

@eriktaubeneck
Last active December 30, 2015 09:19
Show Gist options
  • Save eriktaubeneck/7808280 to your computer and use it in GitHub Desktop.
Save eriktaubeneck/7808280 to your computer and use it in GitHub Desktop.
"all but last n" function for generators
from itertools import tee, imap, izip, islice
from operator import itemgetter
def all_but_last_n(it,n):
it1, it2 = tee(it,2)
return imap(itemgetter(0),izip(it1,islice(it2,n,None)))
assert [0,1,2] == list(all_but_last_n(xrange(5),2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment