zip() Creates an One-time Iterator

 

zip() stacks two lists and creates an one-time iterator. This iterator could be used only once.

a = range(5)
b = range(5)
iter1 = zip(a,b)

print(list(iter1)) # working 
print(list(iter1)) # empty-iterator

Output:

[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]
[]

If we want to reuse the result, we could list() the iterator.

a = range(5)
b = range(5)
iter1 = list(zip(a,b))

print(list(iter1)) # working 
print(list(iter1)) # working 

Output:

[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]