• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
You are here: Home / Python / Sort List of Tuples in Python

Sort List of Tuples in Python

February 26, 2022 Leave a Comment

In Python, we can sort a list of tuples easily. The easiest way to sort a list of tuples is using the sort() function.

list_of_tuples = [('apple', 3), ('pear', 5), ('banana', 1), ('lime', 4)]

list_of_tuples.sort()

print(list_of_tuples)

#Output:
[('apple', 3), ('banana', 1), ('lime', 4), ('pear', 5)]

You can also use sorted to sort a list of tuples in Python.

list_of_tuples = [('apple', 3), ('pear', 5), ('banana', 1), ('lime', 4)]

sorted_list = sorted(list_of_tuples)

print(sorted_list)

#Output:
[('apple', 3), ('banana', 1), ('lime', 4), ('pear', 5)]

In Python, tuples are a collection of objects which are ordered and mutable. When working with a list of tuples, it can be useful to be able to sort the tuples in a list.

To sort a list of tuples in Python, we can use the standard list sort() and sorted() functions.

The Python list sort() function takes a list and sorts it. The Python list sorted() function takes a list as an argument and returns the sorted list.

When sorting a list of tuples, the list is sorted by looking at the first element of the tuple.

Below is an example in Python of how to use sort() to sort a list of tuples.

list_of_tuples = [('apple', 3), ('pear', 5), ('banana', 1), ('lime', 4)]

list_of_tuples.sort()

print(list_of_tuples)

#Output:
[('apple', 3), ('banana', 1), ('lime', 4), ('pear', 5)]

Depending on the situation, you may want to keep the original list and get a new sorted list. You can use sorted() to return the sorted list of tuples as shown in the following Python code.

list_of_tuples = [('apple', 3), ('pear', 5), ('banana', 1), ('lime', 4)]

sorted_list = sorted(list_of_tuples)

print(sorted_list)

#Output:
[('apple', 3), ('banana', 1), ('lime', 4), ('pear', 5)]

How to Sort List of Tuples Descending in Python

When using the sort() or sorted() function to sort a list, the default is that the items will be sorted in ascending order (smallest to biggest, first to last, etc.).

We can easily sort a list of tuples in descending order. To sort a list of tuples in descending order, you just need to pass “reverse=True” to either sort() or sorted().

Below is an example of sorting a list of tuples in descending order with sort() with Python.

list_of_tuples = [('apple', 3), ('pear', 5), ('banana', 1), ('lime', 4)]

list_of_tuples.sort(reverse=True)

print(list_of_tuples)

#Output:
[('pear', 5), ('lime', 4), ('banana', 1), ('apple', 3)]

You can also use the sorted() function to sort a list of tuples in descending order.

list_of_tuples = [('apple', 3), ('pear', 5), ('banana', 1), ('lime', 4)]

sorted_list = sorted(list_of_tuples, reverse=True)

print(sorted_list)

#Output:
[('pear', 5), ('lime', 4), ('banana', 1), ('apple', 3)]

How to Sort List of Tuples by Second Element in Python

You are also easily able to sort a list of tuples by different elements in the tuple. For example, we can easily sort a list of tuples by the second element of each tuple.

To do so, you can pass a lambda expression to the ‘key’ argument of sort() or sorted().

Below is how to sort a list of tuples by the second element in Python using the sort() function.

list_of_tuples = [('apple', 3), ('pear', 5), ('banana', 1), ('lime', 4)]

list_of_tuples.sort(key=lambda t: t[1])

print(list_of_tuples)

#Output:
[('banana', 1), ('apple', 3), ('lime', 4), ('pear', 5)]

You can also use the sorted() function and pass a lambda expression to ‘key’ to sort by the second element of each tuple.

list_of_tuples = [('apple', 3), ('pear', 5), ('banana', 1), ('lime', 4)]

sorted_list = sorted(list_of_tuples, key=lambda t: t[1])

print(sorted_list)

#Output:
[('banana', 1), ('apple', 3), ('lime', 4), ('pear', 5)]

Hopefully this article has been useful for you to learn how to sort a list of tuples in Python.

Other Articles You'll Also Like:

  • 1.  Mastering Python Split Function: A Comprehensive Guide
  • 2.  Using Python to Calculate Average of List of Numbers
  • 3.  Find Last Occurrence in String of Character or Substring in Python
  • 4.  Get First Day of Month Using Python
  • 5.  Using Python to Convert Tuple to String
  • 6.  Symmetric Difference of Two Sets in Python
  • 7.  Get Elapsed Time in Seconds in Python
  • 8.  Convert Set to List in Python
  • 9.  Python Check if List Index Exists Using Python len() Function
  • 10.  Find Maximum of Three Numbers in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · The Programming Expert · About · Privacy Policy