Python
Data Manipulation
Programming Basics
List Indexing
Tuple Operations

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.

python
values = ["a", "b", "c", "d", "e"]
selected = [values[0], values[2], values[4]]
print(selected)

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.

python
1values = ["a", "b", "c", "d", "e"]
2indices = [0, 2, 4]
3
4selected = [values[i] for i in indices]
5print(selected)

This keeps the selection rule separate from the container. It also works the same way for tuples:

python
1values = ("a", "b", "c", "d", "e")
2indices = [1, 3]
3
4selected = [values[i] for i in indices]
5print(selected)

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.

python
1from operator import itemgetter
2
3values = ("a", "b", "c", "d", "e")
4getter = itemgetter(0, 2, 4)
5
6selected = getter(values)
7print(selected)

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:

python
1values = ["a", "b", "c"]
2indices = [0, 2, 5]
3
4selected = [values[i] for i in indices if 0 <= i < len(values)]
5print(selected)

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 IndexError when the indices come from external or uncertain input.
  • Repeating many hard-coded index lookups when an index list or itemgetter would 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.itemgetter is 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.

Course illustration
Course illustration

All Rights Reserved.