[Tutor] help with itertools.izip_longest

Abhishek Pratap abhishek.vit at gmail.com
Sat Mar 16 22:14:30 CET 2013


Hey Guys


I am trying to use itertools.izip_longest to read a large file in
chunks based on the examples I was able to find on the web. However I
am not able to understand the behaviour of the following python code.
(contrived form of example)



for x in itertools.izip_longest(*[iter([1,2,3])]*2):
    print x


###output:
(1, 2)
(3, None)


It gives me the right answer but I am not sure how it is doing it. I
also referred to the itertools doc but could not comprehend much. In
essence I am trying to understand the intracacies of the following
documentation from the itertools package.

"The left-to-right evaluation order of the iterables is guaranteed.
This makes possible an idiom for clustering a data series into
n-length groups using izip(*[iter(s)]*n)."

How is *n able to group the data and the meaning of '*' in the
beginning just after izip.


Thanks!
-Abhi


More information about the Tutor mailing list