Access multiple elements of list knowing their index
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
To access multiple elements from a Python list by their indices, use a list comprehension ([lst[i] for i in indices]), operator.itemgetter, or NumPy array indexing. Python does not support multi-index subscripting on regular lists (like lst[1, 3, 5]), so you need one of these approaches. For performance-critical code with large arrays, NumPy's fancy indexing is the fastest option.
List Comprehension (Most Common)
operator.itemgetter
itemgetter is slightly faster than list comprehension for small numbers of indices because it avoids the overhead of a Python loop.
NumPy Fancy Indexing
map() with list.getitem
This works but using dunder methods directly is generally discouraged. List comprehension is more readable.
Slice for Contiguous Indices
When indices are contiguous, use slicing instead:
Slicing is O(k) where k is the slice length, and it returns a new list (a shallow copy).
Handling Out-of-Range Indices
Performance Comparison
| Method | Speed | Returns | Best For |
| List comprehension | Medium | list | General use, readable |
itemgetter | Fast | tuple | Small index sets, sorting keys |
| NumPy indexing | Fastest | ndarray | Large arrays, numerical data |
map(__getitem__) | Medium | list | Functional style |
| Slicing | Fastest | list | Contiguous ranges only |
Common Pitfalls
- Using a for loop instead of list comprehension: Writing
result = []; for i in indices: result.append(lst[i])works but is slower and more verbose. List comprehension[lst[i] for i in indices]is the Pythonic, faster alternative. - Assuming itemgetter returns a list:
itemgetter(0, 2)(lst)returns a tuple, not a list. If you need a list, wrap it inlist(). Also,itemgetter(0)(lst)with a single index returns the element itself, not a tuple. - Modifying the original list through indexed access:
[lst[i] for i in indices]creates a shallow copy. If the elements are mutable objects (like nested lists), modifying them through the new list also modifies the original. Usecopy.deepcopy()if independence is needed. - Using NumPy for small lists: Converting a small Python list to a NumPy array just for indexing adds overhead that exceeds the benefit. NumPy fancy indexing is only faster for arrays with thousands of elements or more.
- Not validating indices before accessing: Accessing
lst[i]with an out-of-range index raisesIndexError. When indices come from user input or external data, validate them withif 0 <= i < len(lst)before accessing, or use try/except for cleaner error handling.
Summary
- Use list comprehension
[lst[i] for i in indices]for the most readable, general-purpose approach - Use
operator.itemgetter(*indices)for concise extraction and as a sorting key function - Use NumPy fancy indexing
arr[indices]for maximum performance on large numerical arrays - Use slicing (
lst[start:stop:step]) when indices form a contiguous or regularly spaced range - Validate indices before accessing to avoid
IndexErroron out-of-range values - List comprehension and
itemgetterreturn shallow copies — be aware of mutable element references
Related reading
- Access mysql remote database from command line
- access postgres in kubernetes from an application outside the cluster
- Accessing a Dictionary.Keys Key through a numeric index
- Accessing an SQLite Database in Swift
- Access nested dictionary items via a list of keys?
- Accessing class variables from a list comprehension in the class definition
- Accessing dict keys like an attribute?
- Accessing '.pickle' file in Google Colab

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.