[issue35175] Builtin function all() is handling dict() types in a weird way.

Karthikeyan Singaravelan report at bugs.python.org
Tue Nov 6 08:16:59 EST 2018


Karthikeyan Singaravelan <tir.karthi at gmail.com> added the comment:

Dictionary iterates over keys and this is expected behavior. If you need to iterate by values you should use dict().values()

$ python3 -c 'for i in dict(a=1): print(i)'
a
$ python3 -c 'print(all(dict(a=False)))' # Iterate by keys and thus 'a' is True
True
$ python3 -c 'print(all(dict(a=False).values()))' # Iterate by values explicitly
False

# Relevant PEP section :

https://www.python.org/dev/peps/pep-0234/#dictionary-iterators

> Dictionaries implement a tp_iter slot that returns an efficient iterator that iterates over the keys of the dictionary. During such an iteration, the dictionary should not be modified, except that setting the value for an existing key is allowed (deletions or additions are not, nor is the update() method). This means that we can write

> for k in dict: ...

https://stackoverflow.com/questions/3294889/iterating-over-dictionaries-using-for-loops


This is not a bug but an expected behavior unless I am missing something from the script attached

----------
nosy: +xtreak

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


More information about the Python-bugs-list mailing list