I am not sure if this is a bug or a feature. I have a dictionary to be initialized with empty lists.
Lets say
keys =['one','two','three']
sets = dict.fromkeys(keys,[])
What I observed is if you append any item to any of the lists all the lists are modified.
sets = dict.fromkeys(['one','two','three'],[])
sets['one'].append(1)
sets
{'three': [1],'two': [1], 'one': [1]}
But when I do it manually using loop,
for key in keys:
sets[key] = []
sets['one'].append(1)
sets
{'three': [], 'two': [], 'one': [1]}
I would think the second behavior should be the default.
collections.defaultdict. Declare it like this:sets = collections.defaultdict(list).