list in python is empty

Question:

m = ['a', 'b', 'c', 'd']
n = list(range(1,5))
l = zip(m,n)
x = list(l)
print(x)
print(list(l))

The second print is an empty list. Why?

Why not get the same output as the first print?

Asked By: kile

||

Answers:

Zip returns an iterator. Once you consume the iterator then it will continue to return empty.

If you consume the iterator with list(), then you’ll see an empty list as well.

https://realpython.com/python-zip-function/

Answered By: Edward Romero
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.