Python, how to merge three dictionary in one filtering for key?

I have the following dictionaries:

personale_dip={'a': [550.0], 'b': [157.65]} personale_dip_costo={'a': [1.0], 'b': [150.0]} personale_result={'a': 550.0, 'b': 23648.64} 

Personale_result has been obtained with the following code:

personale_result={k : v[0] * personale_dip_costo[k][0] for k, v in personale_dip.items() if k in personale_dip_costo} 

I want to create a new variable, called final_value that, that for each key (matching them across dictionary) summarize all three dictionary giving me the following result:

final_value={'a': [550.0, 1.0, 550.0],   'b': [157.65, 150.0, 23648.64]} 

How should I do it? Any help would be much appreciated. Thanks in advance!

EDIT

I have tried the following code:

from collections import defaultdict  personale_dip = {'a': [550.0], 'b': [157.65]} personale_dip_costo = {'a': [1.0], 'b': [150.0]} personale_result = {'a': 550.0, 'b': 23648.64}  data = defaultdict(list)  for d in [personale_dip, personale_dip_costo, personale_result]:     for k, v in d.items():         if isinstance(v, list):             data[k].extend(v)         else:             data[k].append(v)  

but the result is

defaultdict(<class 'list'>, {'a': [550.0, 1.0, 550.0], 'b': [157.65, 150.0, 23648.64]})  

If I want to obtain only

{'a': [550.0, 1.0, 550.0], 'b': [157.65, 150.0, 23648.64]} 

How could I get it?

Add Comment
3 Answer(s)

Well, you where on the right track. Personally I would simply merge them:

final_value = {} for key in personale_result:     final_value[key] = (personale_dip[key][0], personale_dip_costo[key][0], personal_result[key]) 

With your input this will give you:

>>> final_result {'a': [550.0, 1.0, 550.0], 'b': [157.65, 150.0, 23647.5]} 

Now personale_dip and personale_dip_costo are of type {string, list}. Currently your personale_result will simply ignore other values in that list, as will final_result.

Answered on July 16, 2020.
Add Comment

ANSWER 2.0

personale_dip={'a': [550.0], 'b': [157.65]} personale_dip_costo={'a': [1.0], 'b': [150.0]} personale_result={'a': 550.0, 'b': 23648.64}  final_value = {}  for dictionary in [personale_dip, personale_dip_costo, personale_result]:     for k,v in dictionary.items():         # if the key is there, then          # check whether it a list or a normal string         # if list add it like [] + []         # if not then add it like [] + [v]         if k in final_value:             if isinstance(v, type([])): final_value[k] += v             else: final_value[k] += [v]         else:             if isinstance(v, type([])): final_value[k] = v             else: final_value[k] = [v] print(final_value) 

OUTPUT

>>> {'a': [550.0, 1.0, 550.0], 'b': [157.65, 150.0, 23648.64]} 

This is edited, and gives you what you want.

Add Comment
personale_dip = {'a': [550.0], 'b': [157.65]} personale_dip_costo = {'a': [1.0], 'b': [150.0]} personale_result = dict() for key_pd in personale_dip:     if key_pd in personale_dip_costo.keys():         personale_result[key_pd] = [personale_dip[key_pd][0], personale_dip_costo[key_pd][0], personale_dip[key_pd][0]*personale_dip_costo[key_pd][0]] 

This code should work considering the list inside the dict contains only 1 element. This will work even if personale_dip has more elements or vice versa.

Answered on July 16, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.