Python | Minimum key equal pairs

Sometimes, while working with Python, we can have a problem in which we need to get all the records. This data can have similar values and we need to find minimum key’ed value pair. This kind of problem can occur while working with data. Let’s discuss certain ways in which this task can be done.

Method #1 : Using min() + groupby() + itemgetter() + list comprehension

The combination of above functions can be used to perform this particular task. In this, we first group the like valued elements using groupby() and itemgetter(), and then extract the minimum of those using min() and cummulate the result in list using list comprehension.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Minimum key equal pairs
# using min() + groupby() + itemgetter() + list comprehension
from operator import itemgetter
from itertools import groupby
  
# initialize list
test_list = [(4, 3), (2, 3), (3, 10), (5, 10), (5, 6)]
  
# printing original list
print("The original list : " + str(test_list))
  
# Minimum key equal pairs
# using min() + groupby() + itemgetter() + list comprehension
res = [min(values) for key, values in groupby(test_list, key = itemgetter(1))]
  
# printing result
print("Minimum key equal pairs : " + str(res))

chevron_right


Output :

The original list : [(4, 3), (2, 3), (3, 10), (5, 10), (5, 6)]
Minimum key equal pairs : [(2, 3), (3, 10), (5, 6)]

 



Method #2 : Using setdefault() + items() + loop + list comprehension

The combination of above functions can also achieve this task. In this, we convert the list tuple key-value pairs into a dictionary and assign a default value using setdefault(). The final result is computed using list comprehension.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Minimum key equal pairs
# using setdefault() + items() + loop + list comprehension
  
# initialize list
test_list = [(4, 3), (2, 3), (3, 10), (5, 10), (5, 6)]
  
# printing original list
print("The original list : " + str(test_list))
  
# Minimum key equal pairs
# using setdefault() + items() + loop + list comprehension
temp = {}
for val, key in test_list:
    if val < temp.setdefault(key, val):
        temp[key] = val
res = [(val, key) for key, val in temp.items()]
  
# printing result
print("Minimum key equal pairs : " + str(res))

chevron_right


Output :

The original list : [(4, 3), (2, 3), (3, 10), (5, 10), (5, 6)]
Minimum key equal pairs : [(2, 3), (3, 10), (5, 6)]



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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.