-3

I just started learning Python and I'm trying to concatenate dictionaries using this code:

dict1= {1: 10, 2: 203, 3: 1456}
dict2 = {1: 34, 2: 2034, 3: 176}
dict3 = {1: 134, 2: 2340, 3: 126}
finaldict = {**dict1,**dict2,**dict3}
print(finaldict)

but it is printing only {1: 134, 2: 2340, 3: 126}

What is wrong with this?

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
KMittal
  • 602
  • 1
  • 7
  • 21

5 Answers5

3

The keys 1, 2, and 3 are only allowed once per dictionary. The dictionary construction:

finaldict = {**dict1,**dict2,**dict3}

Adds the keys and values to finaldict from left to right. So it starts with dict1 and has the keys and values of

finaldict = {1: 10, 2: 203, 3: 1456}

Now it moves on dict2, which has values for 1, 2, and 3 as well, thereby overwriting 10, 203, and 1456 with the values 34, 2034, and 176 respectively. And similarly for when it ingests dict3.

James
  • 32,991
  • 4
  • 47
  • 70
2

There is nothing wrong, the last added values will replace other values.

Václav Struhár
  • 1,739
  • 13
  • 21
1

Dictionaries cannot have duplicate keys, therefore, the values of the third dictionary replace / overwrite the other values because they're evaluated from left to right, starting from dict1, then dict2 and dict3.

If you dictionaries would be, for example:

dict1= {9: 10, 8: 203, 7: 1456}
dict2 = {6: 34, 5: 2034, 4: 176}
dict3 = {3: 134, 2: 2340, 1: 126} 

The output would be:

{9: 10, 8: 203, 7: 1456, 6: 34, 5: 2034, 4: 176, 3: 134, 2: 2340, 1: 126}

But since the keys of all the dictionaries are identical, they are just replaced by the new value.


For this case, I would use an array of tuples (key-value pairs) to handle this better, avoiding overwriting:

dict1= {1: 10, 2: 203, 3: 1456}
dict2 = {1: 34, 2: 2034, 3: 176}
dict3 = {1: 134, 2: 2340, 3: 126}
finalDict = list(dict1.items()) + list(dict2.items()) + list(dict3.items())
print(finalDict)

Output:

[(1, 10), (2, 203), (3, 1456), (1, 34), (2, 2034), (3, 176), (1, 134), (2, 2340), (3, 126)]

Another alternative is to use a dictionary comprehension to form a list of values for each key, as follows:

dict1 = {1: 10, 2: 203, 3: 1456}
dict2 = {1: 34, 2: 2034, 3: 176}
dict3 = {1: 134, 2: 2340, 3: 126}
newDict = {key: [value, dict2[key], dict3[key]] for key, value in dict1.items()}
print(newDict)

Output:

{1: [10, 34, 134], 2: [203, 2034, 2340], 3: [1456, 176, 126]}
Mr. Xcoder
  • 4,719
  • 5
  • 26
  • 44
0

The problem is that you can't concatenate two or more dictionaries with duplicated keys. If you don't want to change your key names, you should append them in a list

dict_list = []
dict_list.append(dict)
Federico Lolli
  • 163
  • 1
  • 12
0

On the offchance that what you expected was that the values of the duplicate keys would be summed, here is a way to do that

finaldict = {}
for d in (dict1,dict2,dict3):
    for k,v in d.items():
        if k in finaldict:
            finaldict[k] += v
        else:
            finaldict[k] = v

There's a directory-like Counter() object in collections which lets you use += even on a non-existent key. I'll let you look that up when you are ready.

nigel222
  • 7,582
  • 1
  • 14
  • 22