A bit of Clojure in Python thanks to the itertools (islice
) module:
def take(n, iterable):
"Return first n items of the iterable as a list"
return list(islice(iterable, n))
def nth(iterable, n, default=None):
"Returns the nth item or a default value"
return next(islice(iterable, n, None), default)