Python | Minimum K records of Nth index in tuple list

Sometimes, while working with data, we can have a problem in which we need to get the minimum of elements filtered by the Nth element of record. This has a very important utility in web development domain. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using filter() + lambda + set() + list comprehension
The combination of above functions can be used to perform this particular function. In this, we first filter the min K elements from Nth index and then apply this values to the list and return the result.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Minimum K records of Nth index in tuple list
# Using filter() + lambda + set() + list comprehension
  
# initialize list 
test_list = [('gfg', 4, 'good'), ('gfg', 2, 'better'), 
              ('gfg', 1, 'best'), ('gfg', 3, 'geeks')]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initialize N 
N = 1
  
# initialize K 
K = 2
  
# Minimum K records of Nth index in tuple list
# Using filter() + lambda + set() + list comprehension
temp = set(list({sub[N] for sub in test_list})[ :K])
res = list(filter(lambda sub: sub[N] in temp, test_list))
  
# printing result
print("Min K elements of Nth index are : " + str(res))

chevron_right


Output :



The original list is : [(‘gfg’, 4, ‘good’), (‘gfg’, 2, ‘better’), (‘gfg’, 1, ‘best’), (‘gfg’, 3, ‘geeks’)]
Min K elements of Nth index are : [(‘gfg’, 2, ‘better’), (‘gfg’, 1, ‘best’)]

 

Method #2 : Using groupby() + sorted() + loop
This task can also be performed using above functionalities. In this, we first group the min K elements together and then limit by K while constructing the result list.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Minimum K records of Nth index in tuple list
# Using groupby() + sorted() + loop
import itertools
  
# initialize list 
test_list = [('gfg', 4, 'good'), ('gfg', 2, 'better'),
              ('gfg', 1, 'best'), ('gfg', 3, 'geeks')]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initialize N 
N = 1
  
# initialize K 
K = 2
  
# Minimum K records of Nth index in tuple list
# Using groupby() + sorted() + loop
res = []
temp = itertools.groupby(sorted(test_list, key = lambda sub : sub[N]), 
                                            key = lambda sub : sub[N])
  
for i in range(K):
    res.extend(list(next(temp)[N]))
  
# printing result
print("Min K elements of Nth index are : " + str(res))

chevron_right


Output :

The original list is : [(‘gfg’, 4, ‘good’), (‘gfg’, 2, ‘better’), (‘gfg’, 1, ‘best’), (‘gfg’, 3, ‘geeks’)]
Min K elements of Nth index are : [(‘gfg’, 2, ‘better’), (‘gfg’, 1, ‘best’)]



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.