Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append value to one list in dictionary appends value to all lists in dictionary

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!

like image 431
Gyan Veda Avatar asked Apr 30 '14 20:04

Gyan Veda


People also ask

How do you append a dictionary as a value to another dictionary in python?

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.

Can you append to a list in a dictionary python?

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.

How do you add all the values to a 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.


1 Answers

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.

like image 180
Jon Clements Avatar answered Oct 03 '22 18:10

Jon Clements