Why does Zip return a tuple?

I just went through the exercise attached to this article https://www.codecademy.com/courses/learn-python-3/articles/zip-for-lists and watched the video about tuples in general https://www.codecademy.com/courses/learn-python-3/videos/learn-python3-tuples

And I get the concept of the tuple, I’m just wondering WHY zipping two lists returns a tuple?

I think zip returns a zip object, not a tuple in Python 3. (A zip object is a type of iterator, I think; so it gets “consumed” when you use it to iterate.)

1 Like

It returns a zip object which is an iterator of tuples. The zip object can only be iterated once, then it is used up. It is fairly common to use the list constructor on the zip object to have a list of tuples that can be iterated as often as necessary. Since tuples are immutable, they seem a good fit to protect the elements that were zipped together.

a = ['a', 'b', 'c'] b = [1, 2, 3] c = zip(a,b) #c is now a zip object for t in c: print(t) #print each tuple # try to print them again: for t in c: print(t) #nothing prints because the zip object is empty print("They didn't print a second time.") #this time convert the zip object to a list d = list(zip(a,b)) for t in d: print(t) #prints each tuple #try again for t in d: print(t) #works this time because d is a list not a zip object print("They printed both times.")
1 Like