[issue37164] dict creation with converted zip objects produces inconsistent behavior

Paul Moore report at bugs.python.org
Wed Jun 5 11:00:03 EDT 2019


Paul Moore <p.f.moore at gmail.com> added the comment:

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).

----------
resolution:  -> not a bug

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue37164>
_______________________________________


More information about the Python-bugs-list mailing list