[Tutor] help with itertools.izip_longest

Peter Otten __peter__ at web.de
Sat Mar 16 22:53:12 CET 2013


Abhishek Pratap wrote:

> 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.

Break the expression into smaller chunks:

items = [1, 2, 3]
it = iter(items)
args = [it] * 2 # same as [it, it]
chunks = itertools.izip_longest(*args) # same as izip_longest(it, it)

As a consequence of passing the same iterator twice getting the first item 
from the "first" iterator will advance the "second" iterator (which is 
actually the same as the first iterator) to the second item which will in 
turn advance the "first" iterator to the third item. Try to understand the 
implementation given for izip() at

http://docs.python.org/2/library/itertools.html#itertools.izip

before you proceed to izip_longest().



More information about the Tutor mailing list