classification
Title: Issue in Dictionary fromkeys
Type: behavior Stage: resolved
Components: Windows Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: martin.panter, paul.moore, prudvibt81, steve.dower, tim.golden, zach.ware
Priority: normal Keywords:

Created on 2017-02-14 09:27 by prudvibt81, last changed 2017-02-14 12:10 by prudvibt81. This issue is now closed.

Messages (6)
msg287747 - (view) Author: Prudvi Mangadu (prudvibt81) Date: 2017-02-14 09:27
If we load the keys and values using fromkeys on dic.
Later the modification in the values is reflects for all keys in the DIC, please follow the below code.

#######################################################################################
PS C:\Python27> py
Python 2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> input = ['1','2']
>>> result = {}
>>> result = result.fromkeys(input,[])  >>>> Issue with this line
>>>
>>> result
{'1': [], '2': []}
>>>
>>> result['1'].append("item1")
>>>
>>> result
{'1': ['item1'], '2': ['item1']}   >>>>>>>>>>>>>>>>>> Here result['2'] also overwrites
>>>
>>> result['1'].append("item2")    
>>> result
{'1': ['item1', 'item2'], '2': ['item1', 'item2']}   >>>>>>>>>>>>>>>>>> Here result['2'] also overwrites
>>>
>>>
#######################################################################################
msg287748 - (view) Author: Prudvi Mangadu (prudvibt81) Date: 2017-02-14 09:31
Workaround is replace the below 2 lines with "result ={D_IP:[] for D_IP in input}"

==========================
result = {}
result = result.fromkeys(input,[])  >>>> Issue with this line
==========================
msg287749 - (view) Author: Prudvi Mangadu (prudvibt81) Date: 2017-02-14 09:37
Also observed that ID of the values are pointing same, its ok.
But if some one later modified that values which causes the un-usual output.

>>> id(result['1'])
38121184
>>> id(result['2'])
38121184
msg287770 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2017-02-14 11:55
This is similar to the problem of building a list by repeating one item: <https://docs.python.org/2.7/faq/programming.html#how-do-i-create-a-multidimensional-list>.

With dict.fromkeys(), the resulting dictionary maps each specified key object to the one and only value object (empty list) you passed to fromkeys(). If you look up one of the keys, it returns the list object. If you modify that object, all references to that object will see the update.

The difference with the dictionary comprehension workaround is that the interpreter executes the expressions “D_IP” and “[]” once for each iteration of “input”, resulting in a new empty list instance each iteration. But when you call fromkeys(), you pass a single object. The fromkeys() method just copies references; it does not create objects itself.

If the id() function returns the same identity, it means you have two references to the same underlying object instance. This is the same as checking “result['1'] is result['2']”.
msg287771 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2017-02-14 11:56
I suggest to close this as not a bug.
msg287772 - (view) Author: Prudvi Mangadu (prudvibt81) Date: 2017-02-14 12:10
Thanks Martin , I will close it.
History
Date User Action Args
2017-02-14 12:10:24prudvibt81setstatus: open -> closed

messages: + msg287772
stage: resolved
2017-02-14 11:56:21martin.pantersetresolution: not a bug
messages: + msg287771
2017-02-14 11:55:28martin.pantersetnosy: + martin.panter
messages: + msg287770
2017-02-14 09:37:22prudvibt81setmessages: + msg287749
2017-02-14 09:31:42prudvibt81setmessages: + msg287748
2017-02-14 09:27:46prudvibt81create