Roulette wheel

Peter Pearson ppearson at nowhere.invalid
Fri Mar 6 07:13:56 CET 2009


On 05 Mar 2009 20:43:41 GMT, mattia <gervaz at gmail.com> wrote:
>>     for a, b in zip(*[iter(pop)]*2):
> In the python documentation there is a similar example, well, the obscure 
> thing here is the usage of *[iter(pop)]! Then I believe that I can safely 
> say that you iterate over the values 0 and 1, 2 and 3 etc. because the 
> zip automatically updates the index, in this case using the same sequence 
> the index counter is shared and the second sequence start from that 
> shared counter. Am I right?

I don't think zip updates "the index"; rather, it merely
asks for the next element from each iterator in its argument
list.  But that's probably what you meant.  Just to be sure,
it might be helpful to point out the equivalence of these
two code snippets:

>>> x = [ 1, 2, 3, 4, 5, 6 ]

>>> zip(*[iter(x)]*2)
[(1, 2), (3, 4), (5, 6)]

>>> i = iter(x)
>>> zip(i, i)
[(1, 2), (3, 4), (5, 6)]

So the whole *[iter(x)]*2 construction (which parses as *([iter(x)]*2))
is a trick for getting an argument list comprising several
references to a single iterator.

-- 
To email me, substitute nowhere->spamcop, invalid->net.



More information about the Python-list mailing list