New submission from Jacob Underwood :
I was experimenting with nested dictionaries when I came across strange
behavior that I can not figure out at all. The following function, at least for
me, has some weird behavior. Originally, it was a bit longer, but somehow it
achieved its intended purpose in a strange way, allowing me to cut off the rest
of the correct code and to leave only the following with the code still
continuing to work:
My original intention was to make a function that would use the provided keys
(in the "keys" argument of the following function) in the form of a list to use
all but the last key in the list to get to a nested dictionary within the
provided dictionary arg. Then, using the last key in the list, set the key to
the information provided. Finally, the function should have returned the full
dictionary originally provided, but with the one change within one of the
nested dictionaries of the provided dictionary.
def input_into_nested_dict(dictionary, keys, information):
its = information
cdip = dictionary
for key in keys[:-1]:
cdip = cdip[key]
cdip[keys[-1]] = its
return dictionary
So, an example dictionary of
m = {'a':{'b':{'c':{'d':30
An example list of
y = ['a','b','c','test']
Test of
n = input_into_nested_dict(m, y, 'hello')
The strange thing is, n now correctly has
{'a': {'b': {'c': {'d': 30, 'test': 'hello'
which does not correspond to what this weird cut off function should do, as it
should only provide
{'d': 30, 'test': 'hello'}
Furthermore, somehow this changes the global m, the dictionary provided to the
function, making it
{'a': {'b': {'c': {'d': 30, 'test': 'hello'
(The strange variable names are left over from my original code, though
removing them as they are now unnecessary changes the behavior again, even
though I am pretty sure it should not (?))
Same function without the beginning variables:
def b(dictionary, keys, information):
for key in keys[:-1]:
dictionary = dictionary[key]
dictionary[keys[-1]] = information
return dictionary
Now, when ran with the same m (reset back to the original dictionary before it
was somehow changed), y, and an n of
n = b(m, y, 'hello')
The result is now that n gets the correct dictionary it should get
{'d': 30, 'test': 'hello'}
Though, somehow m changing also continues, with it again getting
{'a': {'b': {'c': {'d': 30, 'test': 'hello'
If this makes no sense, I'm sorry I am new to Python and am not really good
really giving this kind of information
In addition to that, this is probably not even a bug at all and I am probably
just missing something big? If true, I'm sorry about this post...
I do not know if this happens to just me, this originally happened in 3.7.7 but
I tried upgrading and it continued in the most recent version (3.8.3)
Thank you!
--
messages: 369008
nosy: jacob.underwood
priority: normal
severity: normal
status: open
title: Strange behavior in changing nested dictionaries
type: behavior
versions: Python 3.8
___
Python tracker
<https://bugs.python.org/issue40639>
___
___
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com