Explicitly select items from a list or tuple
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Sometimes you do not want a slice or a filter. You want specific positions from a list or tuple, such as items 0, 2, and 5, in that exact order.
The simplest manual approach
If the positions are fixed and few, direct indexing is the clearest solution.
That is explicit, readable, and often perfectly fine. There is no need to over-engineer simple cases.
The only downside is repetition. If the set of positions is dynamic or reused often, a more general pattern is better.
Selecting by a list of indices
When the positions come from data, configuration, or user input, use a comprehension over the desired indices.
This keeps the selection rule separate from the container. It also works the same way for tuples:
Notice that the result above is a list because list comprehensions build lists. If you want a tuple result, wrap the expression with tuple(...).
Using itemgetter
Python also provides operator.itemgetter, which is handy when you want several indexed items at once.
This returns a tuple of the requested items. itemgetter is especially useful when the same selection pattern is applied many times, because you can build the getter once and reuse it.
It is also common in sorting, mapping, or lightweight record extraction code.
Slices are different from explicit selection
A slice selects a continuous range with a step, such as values[1:5:2]. That is useful, but it solves a different problem. Explicit selection means you choose particular positions, not necessarily contiguous ones.
For example, if you need positions 0, 3, and 9, a slice will not express that cleanly. Index-driven selection will.
Understanding that difference helps keep the code honest. Do not force a slice into a job that really wants an index list.
Safety and validation
Every indexed approach can raise IndexError if one of the requested positions does not exist. If the indices are external input, validate them before selecting.
One simple pattern is:
That silently drops invalid positions. In some programs, raising an error is better because it exposes a bad caller. The right choice depends on whether the indices are trusted.
Common Pitfalls
- Using slicing when the requested items are not a contiguous range.
- Forgetting that a comprehension over tuple elements still produces a list unless you convert it.
- Ignoring
IndexErrorwhen the indices come from external or uncertain input. - Repeating many hard-coded index lookups when an index list or
itemgetterwould be clearer. - Assuming explicit selection preserves the original container type automatically.
Summary
- Direct indexing is best when you need a few fixed positions.
- A comprehension over an index list is the most flexible general solution.
- '
operator.itemgetteris convenient when the same explicit selection is reused.' - Slices solve a different problem than arbitrary position selection.
- Validate indices when they are not guaranteed to be in range.

