I have been annoyed by this:
data = ["A:3", "X:-1", "X:2"]
pairs = [data[i].split(":") for i in range(len(data))]
Now the pairs are in a nested list, i.e.:
In[49]: pairs
Out[49]: [['A', '3'], ['X', '-1'], ['X', '2']]
Then let's create dictionaries with two methods, that are supposed to be equivalent:
dico1 = dict.fromkeys([pairs[i][0] for i in range(len(pairs))], [])
dico2 = {pairs[i][0]: [] for i in range(len(pairs))}
We have this now:
dico1
Out[50]: {'A': [], 'X': []}
dico2
Out[51]: {'A': [], 'X': []}
And then let's perform the same processing for both dicts:
for i in range(len(pairs)):
dico1[pairs[i][0]].append(int(pairs[i][1]))
for i in range(len(pairs)):
dico2[pairs[i][0]].append(int(pairs[i][1]))
Yet, I have this:
dico1
Out[54]: {'A': [3, -1, 2], 'X': [3, -1, 2]}
dico2
Out[55]: {'A': [3], 'X': [-1, 2]}
But why?
To me, the expected behaviour is dico2
. Is the fromkeys
method recommended? What happens behind the scenes with this method?
I guess there is a difference inside the memory but more information on this behaviour will be really appreciated.