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.
Output:
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.
In that case, decide on the rule you want.
If you want to skip empty sublists:
If you want a placeholder value:
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.
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.
So the technique is not limited to lists of lists.
A Reusable Helper Function
If this operation appears in several places, wrap it.
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:
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 rowor a conditional expression if empty sublists are possible. - A plain
forloop 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.

