Skip to content
Related Articles

Related Articles

Improve Article
Python – Equable Minimial Records
  • Last Updated : 22 Jun, 2020

Sometimes, while working with Python records, we can have a problem in which we need to extract one of the records that are equal to certain index, which is mimimal of other index. This kind of problem occurs in domains such as web development. Let’s discuss certain ways in which this task can be performed.

Input :
test_list = [(‘Gfg’, 13, 5), (‘is’, 13, 6), (‘best’, 13, 2), (‘CS’, 13, 2)]
eq_idx = 2, min_idx = 3
Output : [(‘best’, 13, 2)]

Input :
test_list = [(‘Gfg’, 12, 5), (‘is’, 12, 6), (‘best’, 13, 2), (‘CS’, 13, 3)]
eq_idx = 2, min_idx = 3
Output : [(‘Gfg’, 12, 5), (‘best’, 13, 2)]

Method #1 : Using list comprehension + min() + lambda
The combination of above functions can be used to solve this problem. In this, we use min() to extract the minimum index value, and list comprehension and lambda functions are used to perform the task of grouping elements.




# Python3 code to demonstrate working of 
# Equable Minimial Records
# Using min() + list comprehension + lambda
  
# initializing list
test_list = [('Gfg', 12, 5), ('is', 13, 6), ('best', 12, 2), ('CS', 13, 2)]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing Equate index 
eq_idx = 2
  
# initializing min index 
min_idx = 3
  
# Equable Minimial Records
# Using min() + list comprehension + lambda
res = [min((ele for ele in test_list if ele[eq_idx - 1] == sub),
      key = lambda a: int(a[min_idx - 1]))
      for sub in {b[eq_idx - 1] for b in test_list}]
  
          
# printing result 
print("Equable Minimal Records : " + str(res)) 
Output :



The original list is : [('Gfg', 12, 5), ('is', 13, 6), ('best', 12, 2), ('CS', 13, 2)]
Equable Minimal Records : [('best', 12, 2), ('CS', 13, 2)]

 

Method #2 : Using groupby() + filter() + lambda
The combination of above functions can be used to solve this problem. In this, we perform the task of grouping using groupby() and filter() + lambda are used for task of matching conditions.




# Python3 code to demonstrate working of 
# Equable Minimial Records
# Using groupby() + filter() + lambda
from itertools import groupby
  
# initializing list
test_list = [('Gfg', 12, 5), ('is', 13, 6), ('best', 12, 2), ('CS', 13, 2)]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing Equate index 
eq_idx = 2
  
# initializing min index 
min_idx = 3
  
# Equable Minimial Records
# Using groupby() + filter() + lambda
res = []
for k, val in groupby(test_list, lambda sub: sub[eq_idx - 1]):
    res.append(min(filter(lambda sub : sub[eq_idx - 1] == k, test_list),
                                   key = lambda sub : sub[min_idx - 1]))
res = list(set(res))
  
          
# printing result 
print("Equable Minimal Records : " + str(res)) 
Output :
The original list is : [('Gfg', 12, 5), ('is', 13, 6), ('best', 12, 2), ('CS', 13, 2)]
Equable Minimal Records : [('best', 12, 2), ('CS', 13, 2)]

 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
Recommended Articles
Page :