Search code examples
pythonlistpython-itertools

Split a python list without calling itertools' grouper function but get a error


In this code the grouper function works fine, however if I do it without calling the function. It throws a error

TypeError: izip_longest argument #1 must support iteration

from itertools import *

def grouper(n, iterable, fillvalue=None):
    args = [iter(iterable)] * n
return izip_longest(fillvalue=fillvalue, *args)


x = [1,2,3]

args = [iter(x)] * 2
l = izip_longest(None , *args )
#l = grouper(2,x)
print [x for x in l]

Solution

  • All positional arguments should be iterables, not fillvalue. Pass fillvalue as a keyword argument:

    it = izip_longest(*iterables, fillvalue=None)
    

    If fillvalue is None; you could omit it:

    it = izip_longest(*iterables)