0

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.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • Note that `input` is the name of a built-in function in Python and it is considered bad practice to name a variable the same, as it can lead to bugs and confusion. I've renamed the variable in your post. – mkrieger1 Apr 10 '21 at 11:04
  • `fromkeys` is a trap. Don't use it. – user2357112 Apr 10 '21 at 11:08

0 Answers0