How to sort list of tuples in Python

Sorting Lists of Tuples in Python: Short guide

In Python, tuples excel at grouping related pieces of information together. When you have a list of these tuples, the ability to sort them based on specific elements becomes crucial for data organization and analysis.

This tutorial will delve into several techniques for sorting lists of tuples in Python, empowering you to arrange your data precisely as needed.

Method 1: The list.sort() Function

Python’s built-in list.sort() function provides a convenient way to sort lists in place. Its key parameter allows you to specify a custom sorting criterion.

list_students = [
    ("Roger", 86),
    ("Rafael", 91),
    ("Novak", 88),
    ("Carlos", 84),
    ("Jannik", 89),
]
# Sort by the second element (score) in ascending order
list_students.sort(key=lambda x: x[1])  
print(list_students)
#Output: [('Carlos', 84), ('Roger', 86), ('Novak', 88), ('Jannik', 89), ('Rafael', 91)]

To sort in descending order, set the reverse parameter to True.

# Sort by the second element (score) in descending order
list_students.sort(key=lambda x: x[1], reverse=True)
print(list_students)
#Output: [('Rafael', 91), ('Jannik', 89), ('Novak', 88), ('Roger', 86), ('Carlos', 84)]

Method 2: The bubble sort algorithm

Bubble Sort, while not the most efficient algorithm, is conceptually simple and easy to implement. It repeatedly compares adjacent elements and swaps them if they are in the wrong order.

You can easily adapt the pos variable to sort by any element within the tuples.

Conclusion

Sorting lists of tuples is a common task in Python, and the language offers elegant solutions. The list.sort() function with a custom key provides a concise and efficient approach, while implementing Bubble Sort demonstrates a fundamental sorting algorithm.

Key takeaways:

  • list.sort(): The sort() function is utilized to sort any given list, with the default manner being in ascending order.
  • Bubble sort algorithm: One of the simplest sorting algorithm, works by continuously swapping one element with its adjacent element if they are in the wrong order till all the elements come to the right order. 

Choose the method that best aligns with your needs and project requirements. Whether you prioritize speed, simplicity, or educational value, Python equips you with the tools to organize your tuple data effectively.

Boost your Python skills by mastering list and string manipulation!

Use AI tools like ChatGPT and Gemini to learn coding efficiently!

You can also use AI tools like Gemini and ChatGPT to recreate the methods mentioned in the article and in more detail. It is free to register on these tools and you do not need any premium membership to use the prompts mentioned below.

sort list of tuples python

python sort list of tuples in adjacent order

List different methods to carry out sort a list of tuples in python

Happy Learning!

Explore more from this category at Python Strings. Alternatively, search and view other topics at All Tutorials.