Sort a List of Tuples in Python

In Python, you can sort a list of tuples using the built-in sorted() function or the sort() method of lists. The sorted() function returns a new sorted list, while sort() modifies the original list in place. You can specify a sorting key using the key parameter to define which element of the tuple should be used for sorting.


Examples

1. Sorting a List of Tuples by First Element

We can sort a list of tuples based on the first element using the sorted() function with the key parameter.

</>
Copy
# Creating a list of tuples
students = [(3, "Alice"), (1, "Bob"), (2, "Charlie")]

# Sorting the list by the first element (ID)
sorted_students = sorted(students, key=lambda x: x[0])

# Printing the sorted list
print("Sorted List:", sorted_students)

Here, we have a list of tuples students where each tuple contains a student ID and name. The sorted() function is used to sort this list based on the first element of each tuple, which represents the student ID. The key=lambda x: x[0] extracts the first element (ID) of each tuple for sorting.

Output:

Sorted List: [(1, 'Bob'), (2, 'Charlie'), (3, 'Alice')]

2. Sorting a List of Tuples by Second Element

We can also sort a list of tuples based on the second element (name in this case) by modifying the key parameter.

</>
Copy
# Creating a list of tuples
students = [(3, "Alice"), (1, "Bob"), (2, "Charlie")]

# Sorting the list by the second element (name)
sorted_students = sorted(students, key=lambda x: x[1])

# Printing the sorted list
print("Sorted List:", sorted_students)

Here, we sort the list of tuples based on the second element (student name). The key=lambda x: x[1] extracts the name from each tuple and sorts them alphabetically.

Output:

Sorted List: [(3, 'Alice'), (1, 'Bob'), (2, 'Charlie')]

3. Sorting a List of Tuples in Descending Order

We can use the reverse=True parameter in sorted() to sort the tuples in descending order.

</>
Copy
# Creating a list of tuples
students = [(3, "Alice"), (1, "Bob"), (2, "Charlie")]

# Sorting the list in descending order by first element
sorted_students = sorted(students, key=lambda x: x[0], reverse=True)

# Printing the sorted list
print("Sorted List:", sorted_students)

Here, we sort the tuples based on the first element (ID) but in descending order by setting reverse=True. The sorting is performed from highest to lowest.

Output:

Sorted List: [(3, 'Alice'), (2, 'Charlie'), (1, 'Bob')]

4. Sorting a List of Tuples with sort() Method

The sort() method sorts a list in place, modifying the original list instead of creating a new one.

</>
Copy
# Creating a list of tuples
students = [(3, "Alice"), (1, "Bob"), (2, "Charlie")]

# Sorting the list in place by first element
students.sort(key=lambda x: x[0])

# Printing the sorted list
print("Sorted List:", students)

The sort() method modifies the original students list in place instead of returning a new list. The sorting key remains the first element of the tuple.

Output:

Sorted List: [(1, 'Bob'), (2, 'Charlie'), (3, 'Alice')]

Conclusion

Sorting a list of tuples in Python can be done using:

  1. sorted(): Returns a new sorted list without modifying the original.
  2. sort(): Sorts the list in place.
  3. Sorting by specific tuple elements: Use key=lambda x: x[index] to sort based on a chosen element.
  4. Descending Order: Use reverse=True for sorting in descending order.

Using the key parameter helps in sorting tuples based on any specific element inside them, making Python sorting flexible and efficient.