zip variable empty after first use
Because zip
returns an iterator in Python 3.x. If you want to re-use it, then make it a list
first:
z = list(zip(t, t2))
zip
returns an iterator (in Python 3). You can only iterate over an iterator once. The iterator doesn't vanish when it's out of elements, but iterating over it again gives 0 elements. If you want a list, call list
on it:
z = list(zip(t, t2))
That's how it works in python 3.x. In python2.x, zip
returned a list of tuples, but for python3.x, zip
behaves like itertools.izip
behaved in python2.x. To regain the python2.x behavior, just construct a list from zip
's output:
z = list(zip(t,t2))
Note that in python3.x, a lot of the builtin functions now return iterators rather than lists (map
, zip
, filter
)