python
tuples
sorting
programming
lists

Sort a list of tuples by 2nd item integer value

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Sorting a list of tuples by the second element is a very common Python task, especially when each tuple represents a name-value pair, a record key plus score, or some other lightweight structure. The clean solution is to give Python a key function that extracts index 1, which is the second item in each tuple.

The Basic sorted Solution

Python's built-in sorted function accepts a key argument. That function is called for each item, and the returned value is what Python uses to order the list.

python
1items = [
2    ("alice", 42),
3    ("bob", 7),
4    ("carol", 19),
5]
6
7result = sorted(items, key=lambda item: item[1])
8print(result)

Output:

text
[('bob', 7), ('carol', 19), ('alice', 42)]

The lambda lambda item: item[1] means "use the second element of each tuple as the sort key."

sorted Versus list.sort

Python gives you two closely related choices:

  • 'sorted(...) returns a new list'
  • 'list.sort(...) sorts the existing list in place'

Example with in-place sorting:

python
1items = [
2    ("alice", 42),
3    ("bob", 7),
4    ("carol", 19),
5]
6
7items.sort(key=lambda item: item[1])
8print(items)

Use sorted when you want to preserve the original list. Use sort when mutating the list is acceptable and you do not need a separate copy.

A Slightly Cleaner Key Function

For this exact pattern, operator.itemgetter is often a little cleaner than a lambda.

python
1from operator import itemgetter
2
3items = [
4    ("alice", 42),
5    ("bob", 7),
6    ("carol", 19),
7]
8
9result = sorted(items, key=itemgetter(1))
10print(result)

This does the same thing as lambda item: item[1], but some readers find it easier to scan.

Descending Order

If you want the largest integer first, add reverse=True.

python
1from operator import itemgetter
2
3items = [
4    ("alice", 42),
5    ("bob", 7),
6    ("carol", 19),
7]
8
9result = sorted(items, key=itemgetter(1), reverse=True)
10print(result)

Output:

text
[('alice', 42), ('carol', 19), ('bob', 7)]

Stability Matters

Python sorting is stable. That means if two tuples have the same second value, their original relative order is preserved.

python
1items = [
2    ("alice", 10),
3    ("bob", 5),
4    ("carol", 10),
5]
6
7result = sorted(items, key=lambda item: item[1])
8print(result)

Output:

text
[('bob', 5), ('alice', 10), ('carol', 10)]

"alice" stays ahead of "carol" because both have the same sort key and Python does not scramble ties unnecessarily.

Sorting by Multiple Fields

Sometimes the second item is the main key, but you also want a tie-breaker. In that case, return a tuple of keys.

python
1items = [
2    ("carol", 10),
3    ("alice", 10),
4    ("bob", 5),
5]
6
7result = sorted(items, key=lambda item: (item[1], item[0]))
8print(result)

This sorts first by the integer in position 1, then by the string in position 0 when the integers match.

Common Pitfalls

The biggest mistake is using the wrong index. Tuple index 1 is the second item, while tuple index 0 is the first.

Another issue is forgetting that list.sort(...) returns None. If you write result = items.sort(...), result will not contain the sorted list.

Developers also sometimes mix incomparable types in the second tuple position. Sorting works cleanly only when the extracted keys can be compared consistently.

Finally, choose between sorted and sort deliberately. One returns a new list, the other mutates the original.

Summary

  • Use sorted(items, key=lambda item: item[1]) to sort by the second tuple element.
  • Use items.sort(...) if you want to sort the list in place.
  • 'operator.itemgetter(1) is a concise alternative to a lambda.'
  • Add reverse=True for descending order.
  • Python sorting is stable, so equal keys keep their original relative order.

Course illustration
Course illustration

All Rights Reserved.