The Problem
I am creating a dictionary with empty lists as values in the following way.
>>> words = dict.fromkeys(['coach', 'we', 'be'], [])
The dictionary looks like this.
>>> words
{'coach': [], 'be': [], 'we': []}
When I append a value to one list, the value is appended to all of them as in this example.
>>> words['coach'].append('test')
{'coach': ['test'], 'be': ['test'], 'we': ['test']}
The Question
My question has two parts. First, why is this happening? Second, what can I do about it? That is, how can I append a value to only one list?
I imagine that in creating the dictionary, I made all lists point to the same object. But I don't understand how that can be because when I input 0
instead of []
in dictionary creation and then add values instead of append them, the values behave differently as if they point to distinct objects.
I would appreciate any input. Thank you in advance!
Append values to a dictionary using the update() method The Python dictionary offers an update() method that allows us to append a dictionary to another dictionary. The update() method automatically overwrites the values of any existing keys with the new ones.
Method 1: Using += sign on a key with an empty value In this method, we will use the += operator to append a list into the dictionary, for this we will take a dictionary and then add elements as a list into the dictionary.
Use sum() to sum the values in a dictionary values() to return the values of a dictionary dict . Use sum(values) to return the sum of the values from the previous step.
dict.fromkeys
uses the same object for all values, in this case a mutable
list... That means, all keys share the same empty list... When you try to .append
to the value of one list, the changes are made in-place to the object, so changes to it are visible by all that reference it.
If instead you used a dict-comp, eg: {k:[] for k in ['could', 'we', 'be']}
each []
is a different empty list and so would be unique for each key value and work as expected.
In regards to using dict.fromkeys(['a', 'b', 'c'], 0)
the 0
is an immutable object thus isn't subject to that gotcha as changes to it result in new objects, not a change to the underlying object which different names (in this case - the values of the different keys) may share.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With