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.
# Python3 code to demonstrate working of # Tuple to Dictionary Summation conversion# Using defaultdict() + loopfrom collections import defaultdict # initializing listtest_list = [(7, 8), (5, 6), (7, 2), (6, 8), (5, 10)] # printing original listprint("The original list is : " + str(test_list)) # Tuple to Dictionary Summation conversion# Using defaultdict() + loopres = defaultdict(int)for sub in test_list: res[sub[0]] += sub[1] # printing result print("The summation tuple dictionary : " + str(dict(res))) |
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.
# Python3 code to demonstrate working of # Tuple to Dictionary Summation conversion# Using dictionary comprehension + sum() + groupby()from itertools import groupby # initializing listtest_list = [(7, 8), (5, 6), (7, 2), (6, 8), (5, 10)] # printing original listprint("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)) |
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. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


