[issue46188] dictionary creation error
TobiasHT
report at bugs.python.org
Tue Dec 28 05:00:03 EST 2021
New submission from TobiasHT <higenyi.tobias at gmail.com>:
I was creating a dictionary when I noticed something weird.
I had a function responsible for creating dictionary keys and then values were assigned to the keys in a for loop.
The code goes a little like this:
>>> def key_maker(n):
... if (n % 2) == 0:
... return n
... return None
>>> dictionary = {}
>>> for i in range(10):
... dictionary[key_maker(i)] = i if key_maker(i) else "error"
>>> dictionary
{0: 'error', None: 'error', 2: 2, 4: 4, 6: 6, 8: 8}
when I tried to print out the rest of the values in the "else" clause,
I realized that 0 appeared there as well, so 0 appeared twice.
>>> for i in range(10):
... dictionary[key_maker(i)] = i if key_maker(i) else print(i)
...
0
1
3
5
7
9
>>> dictionary
{0: 'error', None: 'error', 2: 2, 4: 4, 6: 6, 8: 8}
I still can not figure out why the first two elements are inconsistent
from the rest of the dictionary, and why they appear in the first place.
----------
messages: 409251
nosy: TobiasHT
priority: normal
severity: normal
status: open
title: dictionary creation error
type: behavior
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46188>
_______________________________________
More information about the Python-bugs-list
mailing list