89DEVs

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

How to sort a list of tuples by first element?

To sort a list of tuples by the first element, use the sorted() function or the sort() method of lists as shown below. The difference between sorted() and sort() is: sorted() returns a new list and leaves the original list as it is. sort() is a method of a list and changes the original list.

using sorted() function

To sort a list of tuples b first element, use the sorted() function as shown below. The sorted function returns a new sorted array, the original array is not changed. # sort a list of tuples by first element using sorted() function myList = [('Peter', 5), ('Klaus', 3), ('Alex', 1), ('Mark', 2), ('Tom', 4)] mySortedList = sorted(myList) print(sorted(myList)) In this example, a list of tuples is created and assigned to variable myList. It contains 5 elements with names and ids. Then, the sorted() function is used and the sorted array returned by the function is assigned to the variable mySortedList. Finally the new sorted list is printed and the result is shown below. [('Alex', 1), ('Klaus', 3), ('Mark', 2), ('Peter', 5), ('Tom', 4)] To verify that the original list has not been changed, print it as demonstrated below. # print original list to verify it has not been changed print(myList) The list is printed and contains all the elements in an unchanged position, as we have defined them earlier. [('Peter', 5), ('Klaus', 3), ('Alex', 1), ('Mark', 2), ('Tom', 4)]

using sort() method

To sort a list of tuples by the first element, use the sort() method of Python lists as shown below. # sort a list of tuples by first element using sort() method myList = [('Peter', 5), ('Klaus', 3), ('Alex', 1), ('Mark', 2), ('Tom', 4)] myList.sort() print(myList) First, a list of five elements with tuples containing names and ids is defined and assigned to the variable myList. Then, the sort() method is applied to myList. This changes the original list and doesn't return a new list. Finally, the sorted list myList is printed. [('Alex', 1), ('Klaus', 3), ('Mark', 2), ('Peter', 5), ('Tom', 4)] To sort the list by the second element, in this example the id, use a lambda function as shown below. # sort a list of tuples by second element using sort() method and lambda function myList = [('Peter', 5), ('Klaus', 3), ('Alex', 1), ('Mark', 2), ('Tom', 4)] myList.sort(key = lambda element: element[1]) print(myList) First, the same list with tuples of name and id is defined and assigned to the variable myList. Then, the sort() method is applied to the list. Within the sort() method a lambda function is used to select the second element of the tuple as key. Finally, the sorted list is printed and we can see the list is now ordered by the second element of the tuple, the id. [('Alex', 1), ('Mark', 2), ('Klaus', 3), ('Tom', 4), ('Peter', 5)]

sort() in reverse order

To sort a list of tuples by the first element in reverse order, use the sort() method as shown below. # sort a list of tuples by the first element in reverse order myList = [('Peter', 5), ('Klaus', 3), ('Alex', 1), ('Mark', 2), ('Tom', 4)] myList.sort(reverse=True) print(myList) To sort in reverse the reverse parameter of the sort() method is set to True. This returns the list in reverse order. The result is shown below. [('Tom', 4), ('Peter', 5), ('Mark', 2), ('Klaus', 3), ('Alex', 1)]

                
        

Summary


Click to jump to section