Last active
December 30, 2015 09:19
-
-
Save eriktaubeneck/7808280 to your computer and use it in GitHub Desktop.
"all but last n" function for generators
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
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