You can do this with the fantastic use of itertools.dropwhile
, but I would be embarrassed to call it somehow elegant:
def makepred(n): def pred(x): pred.count += 1 return pred.count < n pred.count = 0 return pred itertools.dropwhile(it, makepred(5))
I really do not recommend this, though - relying on the side effects of the predicate function is very strong on the odd side.
source share