Zipping identical iterables

  • #1
ergospherical
1,032
1,313
Let a be a list (say a = [0,1,2,3,...]), then construct a list of n iterables iter(a),
Python:
it = [iter(a)] * n
When I go to unzip this, e.g.
Python:
for x in zip(*it):
Then x is (0,1,...,n-1), then (n,n+1,...,2n-1), etc.
Just trying to iron out why this works. Because schematically, *it is unpacking it into

*it = iter(a), iter(a), ..., iter(a)

And then zip(*it) should be joining together the first values of each of those n iterables, then the second, etc.

I assume it's because each time iter(a) is seen in zip, the iterable value is updated?
Otherwise x would be (0,0,...,0), then (1,1,...,1) etc.?
 

Similar threads

Back
Top