Related Articles
Python – Maximum of Similar Keys in Tuples
• Last Updated : 10 Jul, 2020

Sometimes, while working with Python tuples, we can have a problem in which we need to perform maximum of all values of the equal keys in Tuple list. This kind of application in useful in many domains such as web development and day-day programming. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [(4, 8), (4, 2), (4, 2), (4, 6)]
Output : [(4, 18)]

Input : test_list = [(1, 8), (2, 2), (3, 6), (4, 2)]
Output : [(1, 8), (2, 2), (3, 6), (4, 2)]

Method #1 : Using `max() + groupby()` + lambda + loop
The combination of above functions can be used to solve this problem. In this, we perform the task of grouping using groupby() and maximum is extrated using max(), and result is compiled using loop.

 `# Python3 code to demonstrate working of ``# Maximum of Similar Keys in Tuples``# Using max() + groupby() + lambda + loop``from` `itertools ``import` `groupby`` ` `# initializing lists``test_list ``=` `[(``4``, ``8``), (``3``, ``2``), (``2``, ``2``), (``4``, ``6``), (``3``, ``7``), (``4``, ``5``)]`` ` `# printing original list``print``(``"The original list is : "` `+` `str``(test_list))`` ` `# Maximum of Similar Keys in Tuples``# Using max() + groupby() + lambda + loop``test_list.sort(key ``=` `lambda` `sub: sub[``0``]) ``temp ``=` `groupby(test_list, ``lambda` `ele: ele[``0``])``res ``=` `[]``for` `key, val ``in` `temp:``    ``res.append((key, ``sum``([ele[``1``] ``for` `ele ``in` `val])))`` ` `# printing result ``print``(``"Maximum grouped elements : "` `+` `str``(res))`
Output :

```The original list is : [(4, 8), (3, 2), (2, 2), (4, 6), (3, 7), (4, 5)]
Maximum grouped elements : [(2, 2), (3, 7), (4, 8)]
```

Method #2 : Using `max() + groupby() + itemgetter()` + list comprehension
The combination of above functions can be used to solve this problem. In this, we perform similar task as above, the second element is chosen using itemgetter and list comprehension is used to compile elements and extract result.

 `# Python3 code to demonstrate working of ``# Maximum of Similar Keys in Tuples``# Using max() + groupby() + itemgetter() + list comprehension``from` `itertools ``import` `groupby``from` `operator ``import` `itemgetter`` ` `# initializing lists``test_list ``=` `[(``4``, ``8``), (``3``, ``2``), (``2``, ``2``), (``4``, ``6``), (``3``, ``7``), (``4``, ``5``)]`` ` `# printing original list``print``(``"The original list is : "` `+` `str``(test_list))`` ` `# Maximum of Similar Keys in Tuples``# Using max() + groupby() + itemgetter() + list comprehension``temp ``=` `groupby(``sorted``(test_list, key ``=` `itemgetter(``0``)), key ``=` `itemgetter(``0``))``res ``=` `[(key, ``max``(``map``(itemgetter(``1``), sub))) ``for` `key, sub ``in` `temp]`` ` `# printing result ``print``(``"Maximum grouped elements : "` `+` `str``(res))`
Output :
```The original list is : [(4, 8), (3, 2), (2, 2), (4, 6), (3, 7), (4, 5)]
Maximum grouped elements : [(2, 2), (3, 7), (4, 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.

My Personal Notes arrow_drop_up