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.
`# 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*

*filter_none*

**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.
`# 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*

*filter_none*

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

## Recommended Posts:

- Python | Replace tuple according to Nth tuple element
- Python - Extract Kth element of every Nth tuple in List
- Python - Sort by Frequency of second element in Tuple List
- Python | Counting Nth tuple element
- Python | Minimum K records of Nth index in tuple list
- Python - Get maximum of Nth column from tuple list
- Python | Insert Nth element to Kth element in other list
- Python - Flatten tuple of List to tuple
- Python - Convert Tuple Matrix to Tuple List
- Python | Sort a tuple by its float element
- Python | Sort tuple based on occurrence of first element
- Python | Sort tuple list on basis of difference of elements
- Python | sort list of tuple based on sum
- Python | Nth tuple index Subtraction by K
- Python - Get Nth column elements in Tuple Strings
- Python - Raise elements of tuple as power to another tuple
- Python - Convert Tuple String to Integer Tuple
- Python - Convert Tuple to Tuple Pair
- Python | Accessing nth element from tuples in list
- Python | Insert after every Nth element in a list

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.