Python | Sort tuple list by Nth element of tuple

Sometimes, while working with Python list, we can come across a problem in which we need to sort list according to any tuple element. These must be a generic way to perform the sort by particular tuple index. This has a good utility in web development domain. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using sort() + lambda
The combination of above functions can be used to perform this task. In this, we just pass a lambda function to sort() with appropriate tuple element index according to which sort has to be performed.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Sort tuple list by Nth element of tuple
# using sort() + lambda
  
# initializing list
test_list = [(4, 5, 1), (6, 1, 5), (7, 4, 2), (6, 2, 4)]
  
# printing original list
print("The original list is : " + str(test_list))
  
# index according to which sort to perform
N = 1
  
# Sort tuple list by Nth element of tuple
# using sort() + lambda
test_list.sort(key = lambda x: x[N])
  
# printing result 
print("List after sorting tuple by Nth index sort : " + str(test_list))

chevron_right


Output :

The original list is : [(4, 5, 1), (6, 1, 5), (7, 4, 2), (6, 2, 4)]
List after sorting tuple by Nth index sort : [(6, 1, 5), (6, 2, 4), (7, 4, 2), (4, 5, 1)]

 

Method #2 : Using sort() + itemgetter()
This is similar to the above method. The difference is just that we use itemgetter(), to perform this task that is done by lambda in above method.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Sort tuple list by Nth element of tuple
# using sort() + itemgetter()
from operator import itemgetter
  
# initializing list
test_list = [(4, 5, 1), (6, 1, 5), (7, 4, 2), (6, 2, 4)]
  
# printing original list
print("The original list is : " + str(test_list))
  
# index according to which sort to perform
N = 1
  
# Sort tuple list by Nth element of tuple
# using sort() + itemgetter()
test_list.sort(key = itemgetter(N))
  
# printing result 
print("List after sorting tuple by Nth index sort : " + str(test_list))

chevron_right


Output :

The original list is : [(4, 5, 1), (6, 1, 5), (7, 4, 2), (6, 2, 4)]
List after sorting tuple by Nth index sort : [(6, 1, 5), (6, 2, 4), (7, 4, 2), (4, 5, 1)]



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.