Python
programming
sublist
list comprehension
list slicing

Extract first item of each sublist in Python

Master System Design with Codemia

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

Introduction

If every sublist has at least one element, the most Pythonic way to extract the first item from each sublist is a list comprehension. The only complication is deciding what to do when some sublists might be empty.

The Standard Answer: List Comprehension

For well-formed input, this is the usual solution.

python
rows = [[1, 2], [3, 4], [5, 6]]
first_items = [row[0] for row in rows]
print(first_items)

Output:

text
[1, 3, 5]

This is concise, readable, and usually the best answer for ordinary Python code.

Why It Works Well

A list comprehension is a good fit because the operation is simple and uniform:

  • loop over each sublist
  • take index 0
  • collect the results into a new list

That is clearer than building a list step by step unless you need more complicated error handling.

Handling Empty Sublists Safely

If some sublists may be empty, the simple version raises IndexError.

python
rows = [[1, 2], [], [5, 6]]
# [row[0] for row in rows]  # IndexError

In that case, decide on the rule you want.

If you want to skip empty sublists:

python
rows = [[1, 2], [], [5, 6]]
first_items = [row[0] for row in rows if row]
print(first_items)

If you want a placeholder value:

python
rows = [[1, 2], [], [5, 6]]
first_items = [row[0] if row else None for row in rows]
print(first_items)

That design choice is more important than the syntax itself.

The Equivalent for Loop

Sometimes a regular loop is easier to read, especially if the extraction logic grows.

python
1rows = [[1, 2], [3, 4], [5, 6]]
2first_items = []
3
4for row in rows:
5    first_items.append(row[0])
6
7print(first_items)

This is not more correct than the comprehension. It is just more expandable if you later need logging, validation, or branching.

Working With Tuples And Other Sequences

The same approach works for nested tuples or mixed sequence-like rows, as long as each inner element supports indexing.

python
rows = [("a", 1), ("b", 2), ("c", 3)]
first_items = [row[0] for row in rows]
print(first_items)

So the technique is not limited to lists of lists.

A Reusable Helper Function

If this operation appears in several places, wrap it.

python
1from typing import Iterable, Sequence, TypeVar
2
3T = TypeVar("T")
4
5
6def first_column(rows: Iterable[Sequence[T]]) -> list[T]:
7    return [row[0] for row in rows]
8
9
10print(first_column([[10, 20], [30, 40], [50, 60]]))

If empty rows are possible, make that behavior explicit in the function rather than hiding it in calling code.

What About zip(*rows)?

You may also see this pattern:

python
rows = [[1, 2], [3, 4], [5, 6]]
first_items = list(zip(*rows))[0]
print(first_items)

It works for rectangular data, but it is usually overkill when you only need the first element of each row. The list comprehension is simpler and more direct.

Performance And Readability

For ordinary Python lists, the comprehension is both fast and readable. There is rarely a reason to reach for NumPy or more advanced tools unless the data is already in a numerical array structure.

In other words, this is one of those cases where the idiomatic answer is also the practical one.

Common Pitfalls

The most common mistake is assuming every sublist has at least one element. If that is not guaranteed, handle the empty case explicitly.

Another mistake is using a more complicated pattern such as zip(*rows) when a simple comprehension is clearer.

Developers also sometimes write a long loop for a one-line transformation that is perfectly suited to a comprehension.

Finally, remember that this code is extracting index 0, not copying entire rows. If the real need is the first column of a rectangular matrix, that is the same thing conceptually, but the data assumptions should still be clear.

Summary

  • The standard Python answer is [row[0] for row in rows].
  • Use if row or a conditional expression if empty sublists are possible.
  • A plain for loop is fine when the logic becomes more complex.
  • The same pattern works for tuples and other indexable inner sequences.
  • Prefer the simple comprehension unless your data shape requires something more advanced.

Course illustration
Course illustration

All Rights Reserved.