• 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 / Sorting a List of Tuples by Second Element in Python

Sorting a List of Tuples by Second Element in Python

February 26, 2022 Leave a Comment

In Python, we can sort a list of tuples by the second element of each tuple easily. The easiest way to sort a list of tuples by the second element is using the sort() function and a lambda expression.

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 sorted to sort a list of tuples by the second element in Python.

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)]

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, by default, the list is sorted by looking at the first element of the tuple.

To sort by the second element of each tuple, 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)]

How to Sort List of Tuples by Second Element 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 by second element in descending order with sort() with Python.

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

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

print(list_of_tuples)

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

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, key=lambda t: t[1], reverse=True)

print(sorted_list)

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

Hopefully this article has been useful for you to learn how to sort a list of tuples in Python by the second element of each tuple.

Other Articles You'll Also Like:

  • 1.  Sort Series in pandas with sort_values() Function
  • 2.  Clear File Contents with Python
  • 3.  rfind Python – Find Last Occurrence of Substring in String
  • 4.  Find Maximum of Three Numbers in Python
  • 5.  Using Python to Capitalize Every Other Letter of String
  • 6.  How to Check if a Letter is in a String Using Python
  • 7.  Using pandas to_csv() Function to Append to Existing CSV File
  • 8.  Print Approximately Equal Symbol in Python
  • 9.  Ceiling Division in Python
  • 10.  Scroll Down Using Selenium 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 *

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