-1
a = [1,2]
b = ["cat", "dog"]
ab = zip(a, b)

for element in ab:
    print(element)

(1, 'cat')
(2, 'dog')

but, I committed an error and wrote this:

for element in ab:
    print(ab)

then I wrote:

for element in ab:
    print(element)

that did not print any output, so my question is, basically, why?

2
  • you have exhausted your iterator, zip is usable only once, if you want to use it more than once, make that a list Commented Apr 15, 2021 at 15:26
  • You were right, thank you Commented Apr 15, 2021 at 15:30

1 Answer 1

0

This is because a and b are of type list, whereas ab is a zip object so doesn't behave in the same way. Try converting ab to a list

AB = list(ab)
print(AB)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.