Question

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!

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top