[Tutor] help with itertools.izip_longest

Abhishek Pratap abhishek.vit at gmail.com
Sat Mar 16 23:03:09 CET 2013


On Sat, Mar 16, 2013 at 2:53 PM, Peter Otten <__peter__ at web.de> wrote:
> 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
>

Thanks Peter. I guess I missed the trick on how each iterator will be
moved ahead automatically as the are basically same, replicated N
times.

-Abhi


> http://docs.python.org/2/library/itertools.html#itertools.izip
>
> before you proceed to izip_longest().
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list