Learning python, came across the zip function and understand that it should build pairs of 2 lists. However, it does not seem to work in my IDE.
a = ['a','b','c']
b = ['p','q','r']
r = zip(a,b)
print(r)
Here I would expect a print of [('a','p'), ('b','q'), ('c','r')]. However, the print output then is: <zip object at 0x011E57D8>
Why is that and what am I doing wrong?
You are using Python 3. In Python 3, zip()
returns an iterator instead of a list.
Iterators evaluation is lazy: only one item is returned at a time, and there is no __len__
. Iterators are made to work best for loop: indeed, most of the time zip()
will be used in a loop, where you only need one item at a time, saving memory and CPU cycles.
Just try to do list(zip(a,b))
to force the evaluation of the iterator into a list.