python chunks iterator 1

python chunks iterator

def grouper(n, iterable, fillvalue=None):
    "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args)

Here is what the above code is Doing:
1. izip_longest is a function that takes an arbitrary number of iterables and
returns an iterator that aggregates elements from each of the iterables.
If the iterables are of uneven length, missing values are filled-in with
fillvalue. izip_longest(‘ABCD’, ‘xy’, fillvalue=’-‘) –> Ax By C- D-
2. The *args in the function definition is saying that the function can be
called with an arbitrary number of arguments, which will then be turned
into a list.
3. The *args in the function call is saying that the list should be expanded
into positional arguments in the function call.

Similar Posts