python - zip variable empty after first use -


python 3.2.3, using idle, python shell

t = (1,2,3) t2 = (5,6,7) z = zip(t,t2)  x in z :     print(x) 

result : (1,5) (2,6) (3,7)

putting in same loop code display z in loop again, after (doing nothing between above , next part) :

for x in z :     print(x) 

result : (blank, in no result)

z still exists,

z 

results in

<zip object @ 0xa8d48ec> 

i can reassign t,t2 zipped again, works once , once, again.

is how supposed work? theres no mention in docs http://docs.python.org/3.2/library/functions.html#zip this.

that's how works in python 3.x. in python2.x, zip returned list of tuples, python3.x, zip behaves itertools.izip behaved in python2.x. regain python2.x behavior, construct list zip's output:

z = list(zip(t,t2)) 

note in python3.x, lot of builtin functions return iterators rather lists (map, zip, filter)


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -