Python – Sort Records by Kth Index List

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 somtimes, 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 : 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Sort Records by Kth Index List
# Using sort() + lambda
  
# initializing list
test_list = [([4, 5, 7, 3], 'Gfg'), ([8, 6, 3, 1], 'is'),
                                 ([2, 3, 5, 2], 'best')]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing K 
K = 1
  
# Sort Records by Kth Index List
# Using sort() + lambda
test_list.sort(key = lambda sub: sub[0][K])
  
# printing result 
print("The records after sorting : " + str(test_list)) 

chevron_right


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

 

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.

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.