Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

python - 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...