This post will discuss how to sort a list of tuples by n’th item in Python.

1. Using list.sort() function

The Pythonic solution to in-place sort a list of tuples is to use the list.sort() function, which can accept two keyword-only arguments: key and reverse. The following example shows how to sort a list of tuples with some items sorted in the reverse order.

Download  Run Code

 
If you don’t need any specific items sorted in the reverse order, you can use the operator.attrgetter() function, which would perform considerably faster.

Download  Run Code

2. Using sorted() function

To get a new sorted list, you can use the sorted() built-in. It accepts the same arguments as the sort() function. This is demonstrated below:

Download  Run Code

That’s all about sorting a list of tuples by n’th item in Python.