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() + 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))) |
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 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)) |
The original list is : [(7, 8), (5, 6), (7, 2), (6, 8), (5, 10)]
The summation tuple dictionary : {7: 10, 5: 16, 6: 8}
Recommended Posts:
- Python - Summation of tuple dictionary values
- Python - Custom Tuple Key Summation in Dictionary
- Python | Tuple key dictionary conversion
- Python | Dictionary to list of tuple conversion
- Python | Summation of list as tuple attribute
- Python | Grouped summation of tuple list
- Python | Summation of Kth Column of Tuple List
- Python | Accumulative index summation in tuple list
- Python - Absolute Tuple Summation
- Python - Cross tuple summation grouping
- Python - Tuple Matrix Columns Summation
- Python - Skew Nested Tuple Summation
- Python - Dual Tuple Alternate summation
- Python | Value summation of key in dictionary
- Python | Summation of dictionary list values
- Python - Dictionary values String Length Summation
- Python - Nested Dictionary values summation
- Python - Summation Grouping in Dictionary List
- Python - Product and Inter Summation dictionary values
- Python - Summation of Custom nested keys in Dictionary
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.

