Reputation: 77
I am in the process of translating my python2 code to python3
One error I am getting is that I need to replace itertools.izip with zip
I did that but I am still getting errors.
I found that these two functions are not really equivalent. Here is an example:
from itertools import izip
for a,b in izip(range(3), "ABC"):
print(a,b)
The above script gave me the following outputounder python2:
(0, 'A')
(1, 'B')
(2, 'C')
If I simply replace izip with zip and run under python3:
for a,b in zip(range(3),"ABC"):
print(a,b)
The output I got is different:
0 A
1 B
2 C
How do I fix my python2 code such that it will generate the same output as before? Thanks a lot in advance.
Upvotes: 1
Views: 478
Reputation: 1620
Upgrading your code with 2to3 and modernize tools could be faster and less error-prone. And now is the right time to start writing tests because print is not a best tool to assert the values.
Upvotes: 0
Reputation: 1868
The behavior you observe isn't caused by the zipping, but by the print
function/statement.
In Python 2, it has this syntax:
print x, ...
In Python 3, it has this syntax:
print(x, ...)
Thus in Python 2, print(a,b)
prints the one argument, tuple (a,b)
. In Python 3, print(a,b)
prints the two arguments a
and b
separately, separated by the default separator (space).
Upvotes: 2