Author martin.panter
Recipients martin.panter, paul.moore, prudvibt81, steve.dower, tim.golden, zach.ware
Date 2017-02-14.11:55:27
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1487073328.37.0.633615936239.issue29552@psf.upfronthosting.co.za>
In-reply-to
Content
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']”.
History
Date User Action Args
2017-02-14 11:55:28martin.pantersetrecipients: + martin.panter, paul.moore, tim.golden, zach.ware, steve.dower, prudvibt81
2017-02-14 11:55:28martin.pantersetmessageid: <1487073328.37.0.633615936239.issue29552@psf.upfronthosting.co.za>
2017-02-14 11:55:28martin.panterlinkissue29552 messages
2017-02-14 11:55:27martin.pantercreate