Python – Custom Tuple Key Summation in Dictionary

Sometimes, while working with Python dictionaries, we can have a problem in which we need to perform group summation of values, of certain key on particular index of tuple keys of dictionary. This problem is quite custom, but can have application in domains that revolves around data processing. Let’s discuss certain ways in which this task can be performed.

Input :
test_dict = {(‘a’, ‘b’): 14, (‘c’, ‘a’): 16, (‘a’, ‘c’): 67}
K = ‘c’, idx = 1
Output : 16

Input :
test_dict = {(‘a’, ‘b’): 14, (‘c’, ‘a’): 16, (‘a’, ‘c’): 67}
K = ‘c’, idx = 2
Output : 67

Method #1 : Using sum() + generator expression
The combination of above functions can be used to solve this problem. In this, we perform summation using sum() and perform task of filtering using generator expression.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Custom Tuple Key Summation in Dictionary
# Using sum() + generator expression
  
# initializing dictionary
test_dict = {('a', 'b'): 14, ('c', 'a'): 16, ('a', 'c'): 67, ('b', 'a'): 17}
  
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
  
# initializing K 
K = 'a'
  
# initializing index
idx = 1
  
# Custom Tuple Key Summation in Dictionary
# Using sum() + generator expression
res = sum(val for key, val in test_dict.items() if key[idx - 1] == K)
      
# printing result 
print("The grouped summation : " + str(res)) 

chevron_right


Output :



The original dictionary is : {('a', 'b'): 14, ('c', 'a'): 16, ('a', 'c'): 67, ('b', 'a'): 17}
The grouped summation : 81

 

Method #2 : Using sum() + map() + lambda
The combination of above functions can be used to solve this problem. In this, we perform the task of perform summation using sum() and map() + lambda is used to perform task of checking conditions.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Custom Tuple Key Summation in Dictionary
# Using sum() + map() + lambda
  
# initializing dictionary
test_dict = {('a', 'b'): 14, ('c', 'a'): 16, ('a', 'c'): 67, ('b', 'a'): 17}
  
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
  
# initializing K 
K = 'a'
  
# initializing index
idx = 1
  
# Custom Tuple Key Summation in Dictionary
# Using sum() + map() + lambda
res = sum(map(lambda sub: sub[1], filter(lambda ele: ele[0][idx - 1] == K,
                                                     test_dict.items())))
      
# printing result 
print("The grouped summation : " + str(res)) 

chevron_right


Output :

The original dictionary is : {('a', 'b'): 14, ('c', 'a'): 16, ('a', 'c'): 67, ('b', 'a'): 17}
The grouped summation : 81



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.