Index all except one item in python
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Indexing "all items except one" is a common Python task when you want to skip a selected element in a list, tuple, string, or NumPy array. The best technique depends on what you need back: a new sequence of values, a list of indexes, or a view-like result in NumPy. For ordinary Python sequences, slicing and comprehension are usually the clearest tools.
Exclude One Position with Slicing
If you know the index to skip, the classic solution is to join the slice before that position with the slice after it.
This works for lists, tuples, and strings:
It is concise and very readable when you are excluding exactly one known position.
Use enumerate When the Logic Is More Explicit
Sometimes the code reads more clearly if you keep the index visible.
This has a few advantages:
- it is easy to extend to more conditions
- it works naturally inside larger transformations
- the "exclude index
skip" rule is explicit
For many real programs, this is more maintainable than slice arithmetic once the rule grows beyond one simple exclusion.
Get the Indexes Instead of the Values
Sometimes you do not want the items themselves. You want all valid indexes except one.
This is useful when the indexes are later used to select from another structure or to build masks.
For example:
NumPy Arrays
With NumPy, boolean masks or np.delete are more natural than Python list slicing patterns.
Or with np.delete:
This is usually the most expressive approach when you are already working in NumPy.
Negative Indexes
Python supports negative indexes, so you should normalize or at least understand them when skipping one element.
This works, but it is easy to get confused when combining negative indexing with slice boundaries. If readability matters, convert the negative index first:
Choose the Right Pattern
Use slicing when:
- you are excluding one known position
- the sequence is a list, tuple, or string
- clarity matters more than generalization
Use enumerate and comprehension when:
- the condition may grow more complex
- you want the logic to be visually explicit
- you are already doing filtering or transformation
Use NumPy masks or np.delete when:
- the data is a NumPy array
- you want idiomatic array operations
Common Pitfalls
The first common mistake is forgetting that slicing creates a new object for lists, tuples, and strings. If you expected in-place removal, that is a different operation.
Another issue is off-by-one errors with skip + 1, especially when the skipped position is the last element.
Negative indexes can also make slicing harder to reason about if they are not normalized first.
Finally, do not use list-style indexing ideas directly on NumPy arrays unless you are sure you want that behavior. NumPy has its own idioms that are often faster and clearer.
Summary
- For standard Python sequences,
seq[:i] + seq[i+1:]is the classic way to exclude one item by index. - List comprehensions with
enumerateare better when the exclusion logic is more explicit or flexible. - Use
rangeif you need indexes rather than values. - For NumPy arrays, prefer boolean masks or
np.delete. - Watch for negative indexes and remember that these operations usually create new sequences.

