a_dict = dict.fromkeys(['a', 'b', 'c'], [])
This creates a dictionary with keys 'a', 'b', 'c' and for each key the value []
.
The issue here is that each key points to the same value: the same empty dict instance []
in memory.
a_dict['a']
and a_dict['b']
are not just equal (evaluating equally), they are the very same instance. The dictionary you created has three keys and the value for each key is a pointer to the same empty dictionary instance.
You can check that:
a_dict['a'] == a_dict['b']
# Returns True: both are equal
a_dict['a'] is a_dict['b']
# Also returns True: both are the same instance
Thus, modifying a_dict['a']
modifies that shared dict
instance and affects a_dict['b']
.