• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
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.  How to Group By Columns and Find Standard Deviation in pandas
  • 2.  Using Python to Count Number of Lines in String
  • 3.  Using Python to Read File Character by Character
  • 4.  Using Python to Find Second Largest Value in List
  • 5.  Does Python Use Semicolons? Why Python Doesn’t Use Semicolons
  • 6.  pandas Correlation – Find Correlation of Series or DataFrame Columns
  • 7.  Python atan2 – Find Arctangent of the Quotient of Two Numbers
  • 8.  Python atanh – Find Hyperbolic Arctangent of Number Using math.atanh()
  • 9.  How to Save and Use Styles from Existing Word File With python-docx
  • 10.  How to Remove All Occurrences of a Character in a List Using 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 *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

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