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.
Choose whether to keep or change the original list
Keep the original unchanged
sorted() returns a new list, leaving records in its original order:
#1 Best Overall
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:
Rank #2
records.sort(key=lambda item: item[1])
Use this when you no longer need the original ordering.
Quick wins for a faster PC:
Scan for outdated or missing drivers - takes under a minuteDriver Scan →Clear out junk files and repair common Windows errorsFree Scan →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:
Do these 3 things before closing this tab:
1Fix the driver behind crashes, sound loss and screen glitches2Repair Windows errors before they cause bigger problems3Scan for outdated or missing drivers - takes under a minutesorted_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.
Best Value
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.
Quick Recap
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.
The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →




