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: Dictionaries of dictionaries behave incorrectly when created from dict.fromkeys()
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.7, Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: josh.r, sourya varenya
Priority: normal Keywords:

Created on 2019-11-28 03:55 by sourya varenya, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg357589 - (view) Author: sourya varenya (sourya varenya) Date: 2019-11-28 03:55
Here's a sample run for reproducibility:
---
Python 3.6.9(Ubuntu 18.04) & 3.7.4(Windows)
---
                                                                                     >>> a = {1:{5:8},2:{5:8}}         
                                                                                     >>> b = dict.fromkeys([1,2],{5:8})   
                                                                                  >>> a                                                                                                                  {1: {5: 8}, 2: {5: 8}}          
                                                                                       >>> b                                                                                                                  {1: {5: 8}, 2: {5: 8}}   
                                                                                              >>> a[1].update({6:9})       
                                                                                          >>> b[1].update({6:9})  
                                                                                               >>> a                                                                                                                  {1: {5: 8, 6: 9}, 2: {5: 8}}     
                                                                                      >>> b                                                                                                                  {1: {5: 8, 6: 9}, 2: {5: 8, 6: 9}}
msg357590 - (view) Author: sourya varenya (sourya varenya) Date: 2019-11-28 04:00
Here's a sample run for reproducibility:
---
Python 3.6.9(Ubuntu 18.04) & 3.7.4(Windows)
---
>>> a = {1:{5:8},2:{5:8}}

>>> b = dict.fromkeys([1,2],{5:8})

>>> a
{1: {5: 8}, 2: {5: 8}}

>>> b
{1: {5: 8}, 2: {5: 8}}

>>> a[1].update({6:9})
>>> b[1].update({6:9})

>>> a
{1: {5: 8, 6: 9}, 2: {5: 8}}

>>> b
{1: {5: 8, 6: 9}, 2: {5: 8, 6: 9}}
msg357591 - (view) Author: Josh Rosenberg (josh.r) * (Python triager) Date: 2019-11-28 04:02
That's the expected behavior, and it's clearly documented here: https://docs.python.org/3/library/stdtypes.html#dict.fromkeys

Quote: "All of the values refer to just a single instance, so it generally doesn’t make sense for value to be a mutable object such as an empty list. To get distinct values, use a dict comprehension instead."
History
Date User Action Args
2022-04-11 14:59:23adminsetgithub: 83115
2019-11-28 04:02:48josh.rsetstatus: open -> closed

nosy: + josh.r
messages: + msg357591

resolution: not a bug
stage: resolved
2019-11-28 04:00:10sourya varenyasetmessages: + msg357590
2019-11-28 03:55:28sourya varenyacreate