Open In App

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. 






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

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’)]



Time Complexity: O(n)
Auxiliary Space: O(n)

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. 




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

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’)]

The time complexity of the given code is O(NlogN + K), where N is the length of the input list and K is the number of minimum elements required.
The auxiliary space complexity of the given code is O(N), where N is the length of the input list. 

Method #3: Using sorted() and slicing

Step-by-step algorithm:

  1. Initialize the input list and print it.
  2. Initialize N and K.
  3. Sort the input list by the Nth index in ascending order.
  4. Select the first K elements from the sorted list using slicing.
  5. Print the resulting list.




# 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 sorted() and slicing
res = sorted(test_list, key=lambda x: x[N])[:K]
 
# printing result
print("Min K elements of Nth index are : " + str(res))

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', 1, 'best'), ('gfg', 2, 'better')]

Time complexity: The time complexity of the sorted() function is O(nlogn), where n is the length of the input list. Slicing a list takes O(k) time where k is the number of elements to slice. Therefore, the time complexity of this approach is O(nlogn + k)
Auxiliary space: O(1).

Method #4: Using heapq.nsmallest()

Step-by-step approach:

Below is the implementation of the above approach:




import heapq
 
# 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 heapq.nsmallest()
res = heapq.nsmallest(K, test_list, key=lambda x: x[N])
 
# printing result
print("Min K elements of Nth index are : " + str(res))

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', 1, 'best'), ('gfg', 2, 'better')]

Time complexity: O(N log K), where N is the length of the list and K is the number of minimum elements to find.
Auxiliary space: O(K), as only the K minimum elements are stored in the res variable.

Method 5: Using a loop and a dictionary

This method involves iterating over the list of tuples and creating a dictionary where the keys are the Nth elements of each tuple, and the values are lists containing the tuples with that Nth element. We then extract the K lowest records by iterating over the keys of the dictionary in sorted order and appending the K lowest tuples from each corresponding value list to a result list.

Step-by-step approach:

Below is the implementation of the above approach:




# Python3 code to demonstrate working of
# Minimum K records of Nth index in tuple list
# Using a loop and a dictionary
 
# 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 a loop and a dictionary
temp_dict = {}
for tup in test_list:
    key = tup[N]
    if key in temp_dict:
        temp_dict[key].append(tup)
    else:
        temp_dict[key] = [tup]
 
res = []
for key in sorted(temp_dict.keys()):
    res += temp_dict[key][:K-len(res)]
    if len(res) >= K:
        break
 
# printing result
print("Min K elements of Nth index are : " + str(res))

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', 1, 'best'), ('gfg', 2, 'better')]

Time complexity: O(N log N) for sorting the dictionary keys and values, where N is the length of the input list.
Auxiliary space: O(N) for storing the dictionary.

Method 6: Using reduce():

Algorithm:

  1. Initialize the list test_list and print it.
  2. Initialize N and K values.
  3. Sort the test_list based on the Nth index using sorted() method and slice the sorted list to get only the first K elements.
  4. Use the reduce() method and a lambda function to iterate through the sliced list and add the elements to the
  5. accumulator if the Nth index is not already present in the accumulator.
  6. The final value of the accumulator will be the required output.
  7. Print the res list.




from functools import reduce
 
# 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 reduce() method and lambda function
res = reduce(lambda acc, x: acc + [x] if x[N] not in [y[N] for y in acc] else acc, sorted(test_list, key=lambda x: x[N])[:K], [])
 
# printing result
print("Min K elements of Nth index are : " + str(res))
#This code is contrinuted by Pushpa.

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', 1, 'best'), ('gfg', 2, 'better')]

Time Complexity: O(N log N + K) (due to sorting and iteration through the list of length K)

Added another approach  (the size of the output list)


Article Tags :