This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: dict creation with converted zip objects produces inconsistent behavior
Type: Stage: resolved
Components: Windows Versions: Python 3.7, Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Dane Howard, paul.moore, steve.dower, tim.golden, zach.ware
Priority: normal Keywords:

Created on 2019-06-05 14:51 by Dane Howard, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
pybug.png Dane Howard, 2019-06-05 14:51 demonstration on windows 10 v3.6.3
Messages (3)
msg344729 - (view) Author: Dane Howard (Dane Howard) Date: 2019-06-05 14:51
confirmed on the following versions:
3.6.3 (Windows 10)
3.7.0 (Debian 9 & Windows 10)
3.7.1 (Debian 9)
a dictionary created with the dict() function will not always return the appropriate dictionary object. The following code produces the bug on all stated versions except 3.6.3/Win10, which exhibits slightly different behavior.

a = ['1','2','3']
b = [1,2,3]
c = zip(a,b)
print(dict(list(c))) #gives empty dict
print(dict(list(zip(a,b)))) #gives {'1':1,'2':2,'3':3}
d = zip(b,a)
print(dict(list(d))) #gives {1:'1',':'2',3:'3'}
msg344730 - (view) Author: Paul Moore (paul.moore) * (Python committer) Date: 2019-06-05 15:00
Works fine for me:

Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a = ['1','2','3']
>>> b = [1,2,3]
>>> c = zip(a,b)
>>> print(dict(list(c)))
{'1': 1, '2': 2, '3': 3}
>>> print(dict(list(zip(a,b))))
{'1': 1, '2': 2, '3': 3}
>>> d = zip(b,a)
>>> print(dict(list(d)))
{1: '1', 2: '2', 3: '3'}

Are you sure you didn't try to use c twice? If you do, it will start up from where it left off (at the end) and so generate no further values:

>>> print(dict(list(c)))
{}

You need to remember that c is an iterator, and this is how iterators work. (If you're coming from Python 2, this is new behaviour in Python 3).
msg344732 - (view) Author: Dane Howard (Dane Howard) Date: 2019-06-05 15:16
Yes, that seems to be it - in every case I was checking the value of c with `list(c)` and assuming that wouldn't change anything.

thanks!
History
Date User Action Args
2022-04-11 14:59:16adminsetgithub: 81345
2019-06-05 15:16:22Dane Howardsetstatus: open -> closed

messages: + msg344732
stage: resolved
2019-06-05 15:00:03paul.mooresetresolution: not a bug
messages: + msg344730
2019-06-05 14:51:24Dane Howardcreate