![]() |
VOOZH | about |
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.
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.
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:
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:
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:
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:
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)