Help with Understanding Pairs in a Zip Function Output

https://www.codecademy.com/paths/computer-science/tracks/cspath-flow-data-iteration/modules/dspath-lists/lessons/create-python-list/exercises/zip

The course mentions that a zip creates a list of lists, but that doesn’t make sense. I think it is suppose to mean a list from lists? or maybe I am reading it wrong. The reason why I bring this up is because there are two items in a pair, but they become one identity (item) in the overall list.

I might be wrong on that conclusion, but let me know what you think pairs are. Are pairs its own lists or an item? I need help with a definite answer on what pairs are.

Thank you,
MP

If it’s python2 zip creates a list of tuples. If you’ve not come across tuples you can just consider them as immutable lists for the time being. Worth looking into in the future.

So your ‘pairs’ are actually tuples of objects from the two separate sequences. python3 is similar but much nicer as it returns a zip object which functions as an iterator where each element is a tuple.

lst1 = [1, 2, 3]
lst2 = ["a", "b", "c"]
zipped_dat = zip(lst1, lst2)
print(zipped_dat)  # for python2
Out: [(1, "a"), (2, "b"), (3, "c")]  # Your 'pairs' are (1, "a"), (2, "b")...

zipped_dat = zip(lst1, lst2)
print(list(zipped_dat))  # for python3
Out: [(1, "a"), (2, "b"), (3, "c")] 
zipped_dat = zip(lst1, lst2)  # Needs to be rebuilt after exhaustion
for item in zipped_dat:
    print(item)
Out: (1, 'a') 
(2, 'b')
(3, 'c')

Bit more info on the zip fucntion-
https://docs.python.org/2.7/library/functions.html#zip
https://docs.python.org/3.8/library/functions.html#zip

In this example:

Result: [(‘Jenny’, ‘Elphonse’), (‘Sam’, ‘Dr.Doggy DDS’), (‘Alexis’, ‘Carter’)]

What is the iterator? Is it ‘Jenny’ or ‘Elphonse’ or both or the whole list?
“Each element of the returned iterator is a tuple of values.” Now, I don’t understand what is the element that is the tuple… Is it (‘Jenny’, ‘Elphonse’)? Or the individual strings?

Sorry, I am having a hard time dissecting which is the iterator and element. Once I know what is being referred to then I can understand zip better.

Thanks,
MP

I’m not sure if it will help you with the zip function at this point. Perhaps I misunderstand your question but you seem more curious about the terminology than the result. I’d encourage you to just use the function and test the outputs and if that’s not enough look for examples of the zip function in use.

If you’re struggling with the terminology you can always look at the glossary-
https://docs.python.org/3/glossary.html#term-iterator

To answer your more direct questions (based on python3 since you asked about the iterator version)-
For your example none of those items are iterator objects. The list itself is an iterable object (most sequences are iterable, e.g. lists, dicts, sets etc.).

The zip function takes two iterables and returns an iterator. In my given example you had two iterable sequences of [1, 2, 3] and [“a”, “b”, “c”]. Calling zip with these two sequences creates an iterator object (zipped_dat). If you were to iterate through every element in zipped_dat you would produce three separate elements. The first element is (1, “a”), a tuple which contains an integer and a single element string, the second would be (2, “b”) as per the example and so on.

In case my use of element still wasn’t clear, an extra example-

for element in ["a", "b", "c"]:
    print(element)  # each element here is just a single string