-1

I have a list called 'A' also have an iterator called 'B', 'B' is the iterator of 'A'.

A = [1, 2, 3, 4, 5, 6, 7, 8, 9]

B = iter(A)

The output of zip(A,A,A) is

[(0, 0, 0),
 (1, 1, 1),
 (2, 2, 2),
 (3, 3, 3),
 (4, 4, 4),
 (5, 5, 5),
 (6, 6, 6),
 (7, 7, 7),
 (8, 8, 8)]

The output of zip(B, B, B) is

[(0, 1, 2), (3, 4, 5), (6, 7, 8)]

Is anybody can explain the difference

DYZ
  • 55,249
  • 10
  • 64
  • 93
Arun
  • 1,149
  • 11
  • 22

1 Answers1

4

It's simple. How does zip work? it creates iterators for each zipped element and goes through them.

When you pass zip(A,A,A), it creates 3 iterators, each will start from A[0].

When you pass zip(B,B,B), you already have an iterator. To create first element of resulting sequence, it calls B.__iter__() 3 times. So you get 0,1,2. For second element it again calls B.__iter() 3 times, thus getting 3,4,5 and so forth.

grapes
  • 8,185
  • 1
  • 19
  • 31