I wanted to learn the functionalities of the zip class. I wrote this very simple example.
>>> names = ['name1','name2','name3']
>>> ages = ['age1','age2','age3']
>>> print(zip(names, ages))
<zip object at 0x03DB18F0>
>>> zipped = zip(names, ages)
for i in zipped:
type(i)
print(i)
and the output is (as expected) -
<class 'tuple'>
('name1', 'age1')
<class 'tuple'>
('name2', 'age2')
<class 'tuple'>
('name3', 'age3')
However immediately after this line if i write:
for i in zipped:
print(i)
it compiles but prints nothing!
To recheck I did this again -
>>> zipped = zip(names, ages)
>>> for i in zipped:
print(i)
('name1', 'age1')
('name2', 'age2')
('name3', 'age3')
This time it prints correctly. But while doing unzip -
>>> names2, ages2 = zip(*zipped)
Traceback (most recent call last):
File "<pyshell#29>", line 1, in <module>
names2, ages2 = zip(*zipped)
ValueError: not enough values to unpack (expected 2, got 0)
It seems the zipped
variable becomes empty for some reason?
Note: if required you may change the title of the question. I am using python 3.6.1 on a windows (10) machine.