Related Articles
Python – Custom Tuple Key Summation in Dictionary
• Last Updated : 22 Jun, 2020

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.

 `# 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)) `
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.

 `# 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)) `
Output :
```The original dictionary is : {('a', 'b'): 14, ('c', 'a'): 16, ('a', 'c'): 67, ('b', 'a'): 17}
The grouped summation : 81
```

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

My Personal Notes arrow_drop_up