Open In App
Related Articles

Python – Summation of tuple dictionary values

Improve Article
Improve
Save Article
Save
Like Article
Like

Sometimes, while working with data, we can have a problem in which we need to find the summation of tuple elements that are received as values of dictionary. We may have a problem to get index wise summation. Let’s discuss certain ways in which this particular problem can be solved. 

Method #1: Using tuple() + sum() + zip() + values() The combination of above methods can be used to perform this particular task. In this, we just zip together equi index values extracted by values() using zip(). Then find summation using respective function. Finally result is returned as index wise summation as a tuple. 

Python3




# Python3 code to demonstrate working of
# Summation of tuple dictionary values
# Using tuple() + sum() + zip() + values()
 
# Initializing dictionary
test_dict = {'gfg' : (5, 6, 1), 'is' : (8, 3, 2), 'best' : (1, 4, 9)}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Summation of tuple dictionary values
# Using tuple() + sum() + zip() + values()
res = tuple(sum(x) for x in zip(*test_dict.values()))
 
# printing result
print("The summation from each index is : " + str(res))

Output : 

The original dictionary is : {'is': (8, 3, 2), 'best': (1, 4, 9), 'gfg': (5, 6, 1)}
The summation from each index is : (14, 13, 12)

Time complexity: O(n), where n is the total number of elements in all the tuples of the dictionary.
Auxiliary space: O(m), where m is the number of keys in the dictionary, since the program only creates a tuple of size equal to the length of one of the tuples in the dictionary.

Method #2: Using tuple() + map() + values() This is yet another way in which this task can be performed. The difference is that we use map() instead of loop. 

Python3




# Python3 code to demonstrate working of
# Summation of tuple dictionary values
# Using tuple() + map() + values()
 
# Initializing dictionary
test_dict = {'gfg' : (5, 6, 1), 'is' : (8, 3, 2), 'best' : (1, 4, 9)}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Summation of tuple dictionary values
# Using tuple() + map() + values()
temp = []
for sub in test_dict.values():
    temp.append(list(sub))
res = tuple(map(sum, temp))
 
# printing result
print("The summation from each index is : " + str(res))

Output : 

The original dictionary is : {'is': (8, 3, 2), 'best': (1, 4, 9), 'gfg': (5, 6, 1)}
The summation from each index is : (14, 13, 12)

Time complexity: O(nm), where n is the number of key-value pairs in the dictionary, and m is the length of the tuples.

Auxiliary space: O(nm), where n is the number of key-value pairs in the dictionary, and m is the length of the tuples.

Method #3: Using a for loop and indexing

  • First, initialize our dictionary test_dict with some key-value pairs where the values are tuples of three integers.
  • Print the original dictionary using the print() function.
  • Initialize an empty tuple res to store the results of the summation of tuple values from each index.
  • Use a for loop to iterate over a range of integers. We determine the range based on the length of the first tuple in our test_dict dictionary values.
    • Calculate the sum of the i-th index values for all the tuples in the dictionary using a generator expression.
    • Append the sum to the result tuple res.
  • Once the loop completes, we print the result tuple res using the print() function.

Python3




# Python3 code to demonstrate working of
# Summation of tuple dictionary values
# Using a for loop and indexing
 
# Initializing dictionary
test_dict = {'gfg': (5, 6, 1), 'is': (8, 3, 2), 'best': (1, 4, 9)}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Summation of tuple dictionary values
res = ()
for i in range(len(next(iter(test_dict.values())))):
    s = sum(test_dict[k][i] for k in test_dict)
    res += (s,)
 
# printing result
print("The summation from each index is : " + str(res))

Output

The original dictionary is : {'gfg': (5, 6, 1), 'is': (8, 3, 2), 'best': (1, 4, 9)}
The summation from each index is : (14, 13, 12)

Time complexity: O(n*m), where n is the number of tuples in the dictionary values and m is the length of each tuple. 
Auxiliary space: O(m), where m is the length of each tuple. 


Last Updated : 13 Apr, 2023
Like Article
Save Article
Similar Reads
Related Tutorials