October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Sort a List of Tuples by the Second Element in Python

Sort tuples by the second element with a key function, choose a new sorted list or an in-place update, and handle descending order and ties.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use sorted(records, key=lambda item: item[1]) to sort tuples by their second element in ascending order. The index is zero-based, so item[1] selects the second value. Use list.sort() instead if you want to reorder the original list.

Sort by the second element

For example, this sorts a list of fruit-and-number tuples by the number:

As an Amazon Associate I earn from qualifying purchases.

records = [('pear', 3), ('apple', 1), ('plum', 2)]
sorted_records = sorted(records, key=lambda item: item[1])

print(sorted_records)
# [('apple', 1), ('plum', 2), ('pear', 3)]

The key function receives each tuple and returns the value Python should compare. Python’s Sorting HOWTO documents that the key function is called exactly once for each input record: Python Sorting HOWTO.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose whether to keep or change the original list

Keep the original unchanged

sorted() returns a new list, leaving records in its original order:

sorted_records = sorted(records, key=lambda item: item[1])

It accepts an iterable and produces a list, so it is also suitable when the input is not already a list.

Sort the list in place

Call sort() on a list to reorder that list directly. It changes the list and returns None:

records.sort(key=lambda item: item[1])

Use this when you no longer need the original ordering.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use a concise accessor from the standard library

operator.itemgetter(1) selects the same second element as lambda item: item[1]:

from operator import itemgetter

sorted_records = sorted(records, key=itemgetter(1))
# Or, for an in-place sort:
records.sort(key=itemgetter(1))

The official Python 3.14.7 Sorting Techniques documentation includes itemgetter among the standard ways to provide a sort key.

Sort in descending order

Pass reverse=True to either sorting method:

sorted_records = sorted(records, key=itemgetter(1), reverse=True)
# In place:
records.sort(key=itemgetter(1), reverse=True)

Handle ties and add a tie-breaker

Keep tied records in their existing order

Python’s sort is stable: if two tuples have the same second element, their relative order from the input is preserved. This is useful when that input order already has meaning.

Sort by another field when values tie

To sort first by the second element and then by the first, return both values as a tuple key:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
sorted_records = sorted(records, key=lambda item: (item[1], item[0]))
# Equivalent:
sorted_records = sorted(records, key=itemgetter(1, 0))

Python compares the key tuple from left to right: the second element determines the main order, and the first element breaks ties.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Check the input data if sorting raises an error

The simple key expression assumes that every record has an element at index 1 and that the resulting values can be compared with one another. A tuple with fewer than two elements can cause an IndexError; incompatible second-element types can cause a TypeError. Validate the records or convert their values to a consistent, orderable type before sorting.

The standard sorting interfaces and key options are described in the Python 3.14.7 Sorting HOWTO.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

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

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.