Python – Sort Records by Kth Index List
Last Updated :
16 May, 2023
Sometimes, while working with Python Records, we can have a problem in which we need to perform Sorting of Records by some element in Tuple, this can again be sometimes, a list and sorting has to performed by Kth index of that list. This is uncommon problem, but can have usecase in domains such as web development. Let’s discuss certain way in which this task can be performed.
Input : test_list = [([4, 5], ‘Gfg’), ([8, 4], ‘is’), ([2, 3], ‘best’)]
K = 0
Output : [([2, 3], ‘best’), ([4, 5], ‘Gfg’), ([8, 4], ‘is’)]
Input : test_list = [([4, 5], ‘Gfg’), ([8, 4], ‘is’), ([2, 3], ‘best’)]
K = 1
Output : [([2, 3], ‘best’), ([8, 4], ‘is’), ([4, 5], ‘Gfg’)]
Method 1: Using sort() + lambda
The combination of above functions can be used to solve this problem. In this, we perform the task of sorting using sort, the parameter and index by which sorting has to be performed is provided using lambda function.
Python3
test_list = [([ 4 , 5 , 7 , 3 ], 'Gfg' ), ([ 8 , 6 , 3 , 1 ], 'is' ),
([ 2 , 3 , 5 , 2 ], 'best' )]
print ( "The original list is : " + str (test_list))
K = 1
test_list.sort(key = lambda sub: sub[ 0 ][K])
print ( "The records after sorting : " + str (test_list))
|
Output :
The original list is : [([4, 5, 7, 3], ‘Gfg’), ([8, 6, 3, 1], ‘is’), ([2, 3, 5, 2], ‘best’)]
The records after sorting : [([2, 3, 5, 2], ‘best’), ([4, 5, 7, 3], ‘Gfg’), ([8, 6, 3, 1], ‘is’)]
Time Complexity: O(nlogn) where n is the number of elements in the in the list “test_list”. The sort() + lambda is used to perform the task and it takes O(nlogn) time.
Auxiliary Space: O(1) additional space is not required.
Method #2: Using itemgetter() from the operator module
The operator module provides a function itemgetter() that is an efficient alternative to lambda functions when sorting a list of tuples. This function can be used to extract the Kth index element from the sublists in the main list.
Python3
import operator
test_list = [([ 4 , 5 , 7 , 3 ], 'Gfg' ), ([ 8 , 6 , 3 , 1 ], 'is' ), ([ 2 , 3 , 5 , 2 ], 'best' )]
print ( "The original list is : " + str (test_list))
K = 1
test_list.sort(key = operator.itemgetter( 0 , K))
print ( "The records after sorting : " + str (test_list))
|
Output
The original list is : [([4, 5, 7, 3], 'Gfg'), ([8, 6, 3, 1], 'is'), ([2, 3, 5, 2], 'best')]
The records after sorting : [([2, 3, 5, 2], 'best'), ([4, 5, 7, 3], 'Gfg'), ([8, 6, 3, 1], 'is')]
Time complexity: Both the approaches have a time complexity of O(nlogn) as sorting the list takes O(nlogn) time.
Auxiliary space: Both the approaches use O(1) extra space as no extra data structure is used for the sorting algorithm.
Method #3: Using a custom function to sort the list
- Initialize the list of records.
- Define a function that takes a single record as input and returns the value of the Kth index of the list in the record.
- Use the sorted() function to sort the list based on the Kth index value obtained by calling the custom function for each record in the list.
- Print the sorted list.
Python3
test_list = [([ 4 , 5 , 7 , 3 ], 'Gfg' ), ([ 8 , 6 , 3 , 1 ], 'is' ), ([ 2 , 3 , 5 , 2 ], 'best' )]
print ( "The original list is : " + str (test_list))
K = 1
def get_kth_index(record):
return record[ 0 ][K]
sorted_list = sorted (test_list, key = get_kth_index)
print ( "The records after sorting : " + str (sorted_list))
|
Output
The original list is : [([4, 5, 7, 3], 'Gfg'), ([8, 6, 3, 1], 'is'), ([2, 3, 5, 2], 'best')]
The records after sorting : [([2, 3, 5, 2], 'best'), ([4, 5, 7, 3], 'Gfg'), ([8, 6, 3, 1], 'is')]
Time complexity: O(n log n) (where n is the number of records in the list) because of the sorting operation.
Auxiliary space: O(n) (where n is the number of records in the list) to store the sorted list.
Share your thoughts in the comments
Please Login to comment...