(it, n, collection=tuple)
>>> for nums in chunked(range(10), 4):
    ...    print(nums)
    ...
    (0, 1, 2, 3)
    (4, 5, 6, 7)
    (8, 9)
    >>> for nums in chunked(range(10), 4, list):
    ...    print(nums)
    ...
    [0, 1, 2, 3]
    [4, 5, 6, 7]
    [8, 9]
>>> for nums in chunked(range(10), 4):
    ...    print(nums)
    ...
    (0, 1, 2, 3)
    (4, 5, 6, 7)
    (8, 9)
    >>> for nums in chunked(range(10), 4, list):
    ...    print(nums)
    ...
    [0, 1, 2, 3]
    [4, 5, 6, 7]
    [8, 9]
>>> for nums in chunked ( range ( 10 ) 4 ) : ... print ( nums ) ... ( 0 1 2 3 ) ( 4 5 6 7 ) ( 8 9 ) >>> for nums in chunked ( range ( 10 ) 4 list ) : ... print ( nums ) ... [ 0 1 2 3 ] [ 4 5 6 7 ] [ 8 9 ]
def chunked(it, n, collection=tuple):
    """
    >>> for nums in chunked(range(10), 4):
    ...    print(nums)
    ...
    (0, 1, 2, 3)
    (4, 5, 6, 7)
    (8, 9)
    >>> for nums in chunked(range(10), 4, list):
    ...    print(nums)
    ...
    [0, 1, 2, 3]
    [4, 5, 6, 7]
    [8, 9]
    """
    itr = iter(it)
    while True:
        try:
            items = take(n, itr, collection)
        except StopIteration:
            break
        if not items:
            break
        yield items
def chunked ( it , n , collection = tuple ) : itr = iter ( it ) while True : try : items = take ( n , itr , collection ) except StopIteration : break if not items : break yield items