89DEVs

Python: How to sort list of tuples by second element?

How to sort a list of tuples by second element?

To sort a list of tuples by the second element, use the sorted() function or the sort() method of lists. The difference between the sorted() function and the sort() method is: The sorted() function returns a new list and keeps the original list unchanged. The sort() method of lists in Python changes the original list and doesn't return a new list. To sort by the second element instead of the first element, use a lambda function. The key parameter is set to the second element in the tuple, examples are shown below.

sorted() function

To sort a list of tuples by the second element, use the sorted function as shown below. # use sorted() function to sort a list of tuples by the second element myList = [('Peter', 5), ('Klaus', 3), ('Alex', 1), ('Mark', 2), ('Tom', 4)] mySortedList = sorted(myList, key=lambda x: x[1]) print(mySortedList) In this example, a list of tuples is defined and assigned to the variable myList. The tuples contain a name and corresponding id. Then, the sorted function is used to create a new sorted list, which is assigned to the variable mySortedList. The key to sort is set to the second element of the tuple, instead of the first. This is done by using a lambda function as shown above. On default the sorted() function sorts the list based on the first element, not the second. We change this by using the key parameter of the sorted() function. Finally, the sorted list is printed and the result is shown below. [('Alex', 1), ('Mark', 2), ('Klaus', 3), ('Tom', 4), ('Peter', 5)]

sort() method

To sort a list of tuples by the second element, use the sort() method of Python lists as shown below. The difference to the sorted() function is, the original list is changed instead of a new list returned. # use sort() method of lists to sort a list of tuples by the second element myList = [('Peter', 5), ('Klaus', 3), ('Alex', 1), ('Mark', 2), ('Tom', 4)] myList.sort(key=lambda x: x[1]) print(myList) In this example, the same list as above is defined and assigned to the variable myList. It contains tuples consisting of a name and an id. Then, the sort() method is used on the list and the key parameter is defined as the second element of the tuple. This is done by using a lambda function with returns the second element. Finally, the sorted list is returned. Please note that using the sort() method the original list is changed and no new list is returned as in the example above. [('Alex', 1), ('Mark', 2), ('Klaus', 3), ('Tom', 4), ('Peter', 5)]

sort() in reverse order

To sort the list by the second element in reverse order, use the sort() method as shown below. # sort() list by the second element in reverse order myList = [('Peter', 5), ('Klaus', 3), ('Alex', 1), ('Mark', 2), ('Tom', 4)] myList.sort(key=lambda x: x[1], reverse=True) print(myList) In this example, the same code as above is used. The only difference is that the reverse parameter is set to True. This reverses the list order as shown below. [('Peter', 5), ('Tom', 4), ('Klaus', 3), ('Mark', 2), ('Alex', 1)]

                
        

Summary


Click to jump to section