Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python zip object 'disappears' after iterating through?

Total noob question here but I really want to know the answer.

I have no idea why the zip object simply "disappears" after I attempt to iterate through it in its list form: eg.

>>> A=[1,2,3]
>>> B=['A','B','C']
>>> Z=zip(A,B)
>>> list(Z)
>>> [('C', 3), ('B', 2), ('A', 1)]
>>> {p:q for (p,q) in Z}
{1: 'A', 2: 'B', 3: 'C'}
>>> {p:q for (p,q) in list(Z)}
{}
>>> list(Z)
[]

(this is in Python 3.4.2)

Can anybody help?

like image 530
Yibo Yang Avatar asked Feb 08 '15 03:02

Yibo Yang


People also ask

How do I iterate a zip object in Python?

Python's zip() function is defined as zip(*iterables) . The function takes in iterables as arguments and returns an iterator. This iterator generates a series of tuples containing elements from each iterable. zip() can accept any type of iterable, such as files, lists, tuples, dictionaries, sets, and so on.

What does* zip do in Python?

Python zip() Function The zip() function returns a zip object, which is an iterator of tuples where the first item in each passed iterator is paired together, and then the second item in each passed iterator are paired together etc.

How to use zip method in Python?

Python zip FunctionThe zip() function combines the contents of two or more iterables. zip() returns a zip object. This is an iterator of tuples where all the values you have passed as arguments are stored as pairs. Python's zip() function takes an iterable—such as a list, tuple, set , or dictionary —as an argument.


3 Answers

There was a change of behavior between Python2 to Python3:
in python2, zip returns a list of tuples while in python3 it returns an iterator.

The nature of iterator is that once it's done iterating the data - it points to an empty collection and that's the behavior you're experiencing.

Python2:

Python 2.7.9 (default, Jan 29 2015, 06:28:58)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> A=[1,2,3]
>>> B=['A','B','C']
>>> Z=zip(A,B)
>>> Z
[(1, 'A'), (2, 'B'), (3, 'C')]
>>> list(Z)
[(1, 'A'), (2, 'B'), (3, 'C')]
>>> list(Z)
[(1, 'A'), (2, 'B'), (3, 'C')]
>>> list(Z)
[(1, 'A'), (2, 'B'), (3, 'C')]
>>> {p:q for (p,q) in Z}
{1: 'A', 2: 'B', 3: 'C'}
>>> Z
[(1, 'A'), (2, 'B'), (3, 'C')]
>>> Z
[(1, 'A'), (2, 'B'), (3, 'C')]

Python3:

Python 3.4.2 (v3.4.2:ab2c023a9432, Oct  5 2014, 20:42:22)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> A=[1,2,3]
>>> B=['A','B','C']
>>> Z=zip(A,B)
>>> list(Z)
[(1, 'A'), (2, 'B'), (3, 'C')]
>>> list(Z)
[]
>>>
like image 112
Nir Alfasi Avatar answered Nov 15 '22 16:11

Nir Alfasi


zip creates an object for iterating once over the results. This also means it's exhausted after one iteration:

>>> a = [1,2,3]
>>> b = [4,5,6]
>>> z = zip(a,b)
>>> list(z)
[(1, 4), (2, 5), (3, 6)]
>>> list(z)
[]

You need to call zip(a,b) every time you wish to use it or store the list(zip(a,b)) result and use that repeatedly instead.

like image 43
Simeon Visser Avatar answered Nov 15 '22 17:11

Simeon Visser


For python 2 and 3, I use

xzip = zip
zip = lambda *x: list(xzip(*x))

Then zip returns always a list rather than an iterator.

IMO, xzip would have been a better name for zip in python 3.

like image 39
Friedrich Avatar answered Nov 15 '22 15:11

Friedrich