krubsburger
krubsburger

Reputation: 91

How to subtract the value of one dictionary from another and write the result to the third?

I am using Python 3.8. I get information about my elements as two dictionaries. The first dictionary retains the original information. The second dictionary is updated with the last value after a while. I need to subtract the value of the first from the second dictionary and write the result to the third dictionary.

dict1 = {'AAA': ['name1', 'link1', '100'],
         'BBB': ['name2', 'link2', '200'],
         'CCC': ['name3', 'link3', '300']
}


dict2 = {'AAA': ['name1', 'link1', '500'],
         'BBB': ['name2', 'link2', '1000'],
         'CCC': ['name3', 'link3', '1500']
}

Desired output:

dict3 = {'AAA': ['name1', 'link1', '400'],
         'BBB': ['name2', 'link2', '800'],
         'CCC': ['name3', 'link3', '1200']
}

Upvotes: 0

Views: 1601

Answers (5)

Vishal Singh
Vishal Singh

Reputation: 6234

This uses dictionary comprehension to construct a new dictionary by subtracting the keys of dict1 from dict2.

dict3 = {
    k2: v2[:2] + [str(int(v2[-1]) - int(v1[-1]))]
    for (_, v1), (k2, v2) in zip(dict1.items(), dict2.items())
}

Upvotes: 1

Artur Nowicki
Artur Nowicki

Reputation: 461

If you'd like to avoid nested loops then try this:

result = {key: dict1.get(key, 0)[:2] +
          [str(int(dict2.get(key, 0)[2]) - int(dict1.get(key, 0)[2]))]
          for key in set(dict1)}

Upvotes: 1

Rakesh
Rakesh

Reputation: 82765

This is one approach

Ex:

dict3 = {}
for k, v in dict1.items():
    sVal = dict2.get(k)
    nval = v.copy()
    if sVal:
        nval[-1] = int(sVal[-1]) - int(nval[-1]) 
    dict3[k] = nval
    
print(dict3)

Output:

{'AAA': ['name1', 'link1', 400],
 'BBB': ['name2', 'link2', 800],
 'CCC': ['name3', 'link3', 1200]}

Upvotes: 0

Timbolt
Timbolt

Reputation: 206

What you really want to subtract is a value at a specific index in a list from a value at the same index in another list.

One way to do it would be:

from copy import deepcopy

dict3 = deepcopy(dict1) # make a copy to overwrite since most of the info is duplicated anyway

for key in dict1:
    difference_value = int(dict2[key][2]) - int(dict1[key][2])
    dict3[key][2] = str(difference_value)

print(dict3)

Output.

 {'AAA': ['name1', 'link1', '400'], 'BBB': ['name2', 'link2', '800'], 'CCC': ['name3', 'link3', '1200']}

Edit: This assumes that the values you want to subtract stay it the same index in the list (2 or -1 if you want to say always the last index in the list). It also assumes that input and output are integers.

Edit2: Changed it so you are not making changes to dict1 or dict2.

Upvotes: 2

karthik
karthik

Reputation: 34

Assuming the position remains constant in all the values

dict3 = {}
for i in dict1:
    for j in dict2:
        if i == j:
            new_2 = int(dict2[j][-1])- int(dict1[i][-1])
            dict3[i] = [dict2[i][0], dict2[i][1], str(new_2)]

output:

print(dict3)

{'AAA': ['name1', 'link1', '400'],
 'BBB': ['name2', 'link2', '800'],
 'CCC': ['name3', 'link3', '1200']}

Upvotes: 1

Related Questions