Martin Panter added the comment: 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']”. ---------- nosy: +martin.panter _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue29552> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com