0

We have 3 dictionaries with same keys as dates and with different count values for each date.

Input :-

dict1 = {'01/03/22': 7, '02/03/22': 7, '03/03/22': 0, '04/03/22': 7}

dict2 = {'01/03/22': 5, '02/03/22': 1, '03/03/22': 0, '04/03/22': 1}

dict3 = {'01/03/22': 2, '02/03/22': 4, '03/03/22': 0, '04/03/22': 3}

Output :- The final output should be as below.

final_dict = {'01/03/22': 14, '02/03/22': 12, '03/03/22': 0, '04/03/22': 11}

I have tried it using Counter class but it has missed the key-value pair which has 0 count values for the items.

final_dict = dict(Counter(dict1) + Counter(dict2) + Counter(dict3))

It has given me output below:-

final_dict = {'01/03/22': 14, '02/03/22': 12, '04/03/22': 11}

So in above you can see we have missed key value pairs which has 0 counts but as part of request i need to have the values for each date.

Any suggestions?

  • Thanks for suggestion, the only question i have is i am not having a list of dict here so is there any other way of counting values for individual dictionaries. – SMRITI MAHESHWARI Mar 14 '22 at 06:28
  • 1
    You can simply create an ad-hoc list: `my_dicts = [dict1, dict2, dict3]`. The point is that if you want to retain the zero count entries, you have to keep track of them yourself, e.g. by saving the dictionary keys in a separate `set`. – Jan Wilamowski Mar 14 '22 at 06:34

0 Answers0