spam = ['foo', 'bar'] eggs = dict.fromkeys(spam, []) print(eggs) print([id(item) for item in eggs.values()])
Output:{'foo': [], 'bar': []}
[140175770726664, 140175770726664]
lists are mutable and as you can see all keys refer to same object (same id). Update is reflected in all of them.you can do
eggs = {key:[] for key in spam}
alternatively, you can use
collections.defaultdict
with default value being list
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs