ryoakg
8/23/2016 - 9:58 AM

iterator.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import itertools

# http://docs.python.jp/2/library/itertools.html
# http://docs.python.jp/3/library/itertools.html#itertools-recipes
def take(n, iterable):
    return itertools.islice(iterable, n)

def drop(n, iterable):
    return itertools.islice(iterable, n, None)

list(take(3, range(10)))        # [0, 1, 2]
list(drop(3, range(10)))        # [3, 4, 5, 6, 7, 8, 9]