Issue 32242: loop in loop with with 'zip'ped object misbehaves in py3.6 (original) (raw)
Created on 2017-12-07 14:15 by Vishu Viswanathan, last changed 2022-04-11 14:58 by admin. This issue is now closed.
Messages (6)
Author: Vishu Viswanathan (Vishu Viswanathan)
Date: 2017-12-07 14:15
The file shows the results by running in Py3.6 and 2.7 In my opinion Py2.7 results matches what I expected.
In this bug or the zip function behaviour is changed with some purpose
Author: Steven D'Aprano (steven.daprano) *
Date: 2017-12-07 14:23
I'm sorry, I have no idea how to read an Anaconda notebook file. In the browser it looks like some sort of nested dictionary. I can find the code:
j = [1, 2, 3, 4] k = [5, 6, 7, 8] z = zip(j, k) for x, y in z: for m, n in z: print (x, y, m, n)
but I'm not sure what result you are getting or what results you expect.
Author: Ronald Oussoren (ronaldoussoren) *
Date: 2017-12-07 14:29
If the nested loop is the issue the Python 3 version behaves as expected.
A difference between python2 and python3 is that the zip() builtin returns a list on python2 and an iterator on python3. This explains the difference in results in running the code on the two versions.
To get the same behaviour on Python 2 and Python 3 use "list(zip(j, k))".
Author: Serhiy Storchaka (serhiy.storchaka) *
Date: 2017-12-07 14:32
In Python 2 zip() returns a list. In Python 3 it returns an iterator (like itertools.izip() in Python 2). This is an intentional change. For restoring the Python 2 behavior you should wrap the result of zip() into list(): z = list(zip(j, k)). The 2to3 command can do this for you.
Author: Steven D'Aprano (steven.daprano) *
Date: 2017-12-07 14:33
I decided to run the code in 3.5 and 2.7, and now that I know what I'm looking for, I can see the results buried in the Anaconda notebook.
This is not a bug, zip has been changed in Python 3 to return an iterator instead of a list. To get the same results as Python 2.7, change the line:
z = zip(j, k)
to:
z = list(zip(j, k))
To get the same results in 2.7 as in 3, change it to:
z = iter(zip(j, k))
This is documented and is not a bug.
https://docs.python.org/3.0/whatsnew/3.0.html#views-and-iterators-instead-of-lists
https://docs.python.org/3/library/functions.html#zip
Author: Vishu Viswanathan (Vishu Viswanathan)
Date: 2017-12-08 06:04
Thanks for the fast response and clarification. Now I can run my code made for py2.7 also run in py3.6 I should search and read documentation before reporting.
Thanks
On Thu, Dec 7, 2017 at 8:03 PM, Steven D'Aprano <report@bugs.python.org> wrote:
Steven D'Aprano <steve+python@pearwood.info> added the comment:
I decided to run the code in 3.5 and 2.7, and now that I know what I'm looking for, I can see the results buried in the Anaconda notebook.
This is not a bug, zip has been changed in Python 3 to return an iterator instead of a list. To get the same results as Python 2.7, change the line:
z = zip(j, k)
to:
z = list(zip(j, k))
To get the same results in 2.7 as in 3, change it to:
z = iter(zip(j, k))
This is documented and is not a bug.
https://docs.python.org/3.0/whatsnew/3.0.html#views-and- iterators-instead-of-lists
https://docs.python.org/3/library/functions.html#zip
Python tracker <report@bugs.python.org> <https://bugs.python.org/issue32242>
History
Date
User
Action
Args
2022-04-11 14:58:55
admin
set
github: 76423
2017-12-08 06:04:49
Vishu Viswanathan
set
messages: +
2017-12-07 14:33:16
steven.daprano
set
messages: +
2017-12-07 14:32:28
serhiy.storchaka
set
status: open -> closed
nosy: + serhiy.storchaka
messages: +
resolution: not a bug
stage: resolved
2017-12-07 14:29:48
ronaldoussoren
set
nosy: + ronaldoussoren
messages: +
2017-12-07 14:23:18
steven.daprano
set
nosy: + steven.daprano
messages: +
2017-12-07 14:15:53
Vishu Viswanathan
create