Python – Tuple to Dictionary Summation conversion

Sometimes, while working with Python tuples, we can have a problem in which we can have data points in tuples, and we need to convert them to dictionary after performing summation of similar keys. This kind of operation can also be extended to max, min or product. This can occur in data domains. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [(5, 8), (5, 6), (5, 2), (5, 8), (5, 10)]
Output : {5: 34}

Input : test_list = [(5, 8), (9, 6)]
Output : {5: 8, 9: 6}

Method #1 : Using loop + defaultdict()
The combination of above functions can be used to solve this problem. In this, we initialize the counter dictionary with integers using defaultdict() and loop is used to iterate for all data points.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Tuple to Dictionary Summation conversion
# Using defaultdict() + loop
from collections import defaultdict
  
# initializing list
test_list = [(7, 8), (5, 6), (7, 2), (6, 8), (5, 10)]
  
# printing original list
print("The original list is : " + str(test_list))
  
# Tuple to Dictionary Summation conversion
# Using defaultdict() + loop
res = defaultdict(int)
for sub in test_list:
    res[sub[0]] += sub[1]
  
# printing result 
print("The summation tuple dictionary : " + str(dict(res))) 

chevron_right


Output :



The original list is : [(7, 8), (5, 6), (7, 2), (6, 8), (5, 10)]
The summation tuple dictionary : {7: 10, 5: 16, 6: 8}

 

Method #2 : Using dictionary comprehension + sum() + groupby()
The combination of above functions can be used to solve this problem. In this, we perform the task of summation using sum(), groupby() is used to group similar elements for further computations.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Tuple to Dictionary Summation conversion
# Using dictionary comprehension + sum() + groupby()
from itertools import groupby
  
# initializing list
test_list = [(7, 8), (5, 6), (7, 2), (6, 8), (5, 10)]
  
# printing original list
print("The original list is : " + str(test_list))
  
# Tuple to Dictionary Summation conversion
# Using dictionary comprehension + sum() + groupby()
fnc = lambda ele: ele[0]
res = {key: sum(sub[1] for sub in val) for key, val in groupby(
                       sorted(test_list, key = fnc), key = fnc)}
  
# printing result 
print("The summation tuple dictionary : " + str(res)) 

chevron_right


Output :

The original list is : [(7, 8), (5, 6), (7, 2), (6, 8), (5, 10)]
The summation tuple dictionary : {7: 10, 5: 16, 6: 8}

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.