Sort a list of tuples by n’th item in Python
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
if __name__ == '__main__': emp = [('John', 'Telecom', 100000), ('Bob', 'IT', 80000), ('John', 'Finance', 70000)] # 1. Sort by the first item emp.sort(key=lambda x: x[0]) # [('Bob', 'IT', 80000), ('John', 'Telecom', 100000), ('John', 'Finance', 70000)] print(emp) # 2. Sort by the first item, followed by the second item emp.sort(key=lambda x: (x[0], x[1])) # [('Bob', 'IT', 80000), ('John', 'Finance', 70000), ('John', 'Telecom', 100000)] print(emp) # 3. Sort by the first and second item, in reverse order emp.sort(key=lambda x: (x[0], x[1]), reverse=True) # [('John', 'Telecom', 100000), ('John', 'Finance', 70000), ('Bob', 'IT', 80000)] print(emp) # 4. Sort by the first item, followed by the third item (an integer) in reverse order emp.sort(key=lambda x: (x[0], -x[2])) # [('Bob', 'IT', 80000), ('John', 'Telecom', 100000), ('John', 'Finance', 70000)] print(emp) |
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
from operator import itemgetter if __name__ == '__main__': emp = [('John', 'Telecom', 100000), ('Bob', 'IT', 80000), ('John', 'Finance', 70000)] # 1. Sort by the first item emp.sort(key=itemgetter(0)) # [('Bob', 'IT', 80000), ('John', 'Telecom', 100000), ('John', 'Finance', 70000)] print(emp) # 2. Sort by the first item, followed by the third item emp.sort(key=itemgetter(0, 2)) # [('Bob', 'IT', 80000), ('John', 'Finance', 70000), ('John', 'Telecom', 100000)] print(emp) # 3. Sort by the first item, followed by the third item, in reverse order emp.sort(key=itemgetter(0, 2), reverse=True) # [('John', 'Telecom', 100000), ('John', 'Finance', 70000), ('Bob', 'IT', 80000)] print(emp) |
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:
|
1 2 3 4 5 6 7 8 9 10 |
if __name__ == '__main__': emp = [('John', 'Telecom', 100000), ('Bob', 'IT', 80000), ('John', 'Finance', 70000)] # sort by the first item, followed by the second item sortedList = sorted(emp, key=lambda x: (x[0], x[1])) # [('Bob', 'IT', 80000), ('John', 'Finance', 70000), ('John', 'Telecom', 100000)] print(sortedList) |
That’s all about sorting a list of tuples by n’th item in Python.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)