Feature or enhancement
Proposal:
Proposal: Add list.without(i) method to CPython
Proposal
This proposal introduces a new method on Python's built-in list type:
list.without(index: int) -> list
The method returns a new list with the element at the specified index removed. It does not mutate the original list.
Motivation
Current idioms for removing an element by index create unnecessary intermediate lists and are less direct:
a = [1, 2, 3, 4]
b = a[:i] + a[i+1:]
This requires:
- two slice operations
- one concatenation
- multiple temporary list allocations
The proposed method expresses the operation directly and allows a single-pass optimized implementation in C.
Semantics
- Input: integer index
i
- Output: new list containing all elements of the original list except
a[i]
- Original list is unchanged
- Negative indices follow standard Python indexing rules
- Raises
IndexError if index is out of range
Examples
a = [1, 2, 3, 4]
a.without(1)
# [1, 3, 4]
a.without(-1)
# [1, 2, 3]
Equivalent behavior
def without(lst, i):
return lst[:i] + lst[i+1:]
The proposed method allows CPython to implement this in a single allocation and single copy pass.
Implementation Notes
A C implementation in CPython (listobject.c) can:
- allocate a new list of size
n - 1
- copy elements
[0:i]
- copy elements
[i+1:n]
- avoid intermediate Python-level list constructions
Performance Considerations
This method reduces:
- temporary allocations
- Python-level concatenation overhead
- bytecode execution for slicing operations
Alternatives considered
- Slicing + concatenation (current idiom)
- Generator-based construction:
[x for j, x in enumerate(a) if j != i]
- itertools-based filtering
All alternatives are either less efficient or less direct.
Design rationale
The method is intentionally limited to index-based removal to avoid ambiguity between:
- value removal (
remove)
- index removal (
pop)
- set-like subtraction semantics
Naming choice without emphasizes immutability and transformation without mutation.
Backward compatibility
No conflicts with existing Python APIs.
Has this already been discussed elsewhere?
No known prior CPython discussions specifically proposing list.without(i).
Related concepts:
list.pop(i) (mutating removal)
- slicing idioms for reconstruction
- immutable sequence patterns in functional programming
Potential future discussion required on:
- naming (
without, drop, removed)
- possible extension to other sequence types
Primary motivation is from implementing string permutations and having calls like:
permute(prefix+a[i],a[:i] +a[i+1:])
which is uglier than:
permute(prefix+a[i],a.without(i))
### Has this already been discussed elsewhere?
No response given
### Links to previous discussion of this feature:
_No response_
Feature or enhancement
Proposal:
Proposal: Add
list.without(i)method to CPythonProposal
This proposal introduces a new method on Python's built-in
listtype:The method returns a new list with the element at the specified index removed. It does not mutate the original list.
Motivation
Current idioms for removing an element by index create unnecessary intermediate lists and are less direct:
This requires:
The proposed method expresses the operation directly and allows a single-pass optimized implementation in C.
Semantics
ia[i]IndexErrorif index is out of rangeExamples
Equivalent behavior
The proposed method allows CPython to implement this in a single allocation and single copy pass.
Implementation Notes
A C implementation in CPython (
listobject.c) can:n - 1[0:i][i+1:n]Performance Considerations
This method reduces:
Alternatives considered
All alternatives are either less efficient or less direct.
Design rationale
The method is intentionally limited to index-based removal to avoid ambiguity between:
remove)pop)Naming choice
withoutemphasizes immutability and transformation without mutation.Backward compatibility
No conflicts with existing Python APIs.
Has this already been discussed elsewhere?
No known prior CPython discussions specifically proposing
list.without(i).Related concepts:
list.pop(i)(mutating removal)Potential future discussion required on:
without,drop,removed)Primary motivation is from implementing string permutations and having calls like:
which is uglier than: