erase 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,43 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project are documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ ## [0.1.0] - 2026-12-04
11
+
12
+ Initial release.
13
+
14
+ ### Added
15
+
16
+ - `erase(seq, value, /)`: removes every occurrence of `value` from the mutable
17
+ sequence `seq` in place and returns the number of items removed (0 if none).
18
+ An item `x` is removed when `x is value or x == value`, the test
19
+ `list.remove()` uses, and each item is compared exactly once.
20
+ - Atomic behaviour: if a comparison raises, the exception propagates and `seq`
21
+ is left unchanged. If `seq` is modified during the comparisons so that its
22
+ length changes or a matched item is replaced, `RuntimeError` is raised and
23
+ `seq` is left unchanged.
24
+ - C extension `erase._erase` for lists and for list subclasses that do not
25
+ override `__setitem__` or `__delitem__`: one comparison pass, then in-place
26
+ compaction. It supports free-threaded CPython, and on GIL builds it releases
27
+ a list's spare capacity after large removals.
28
+ - Generic path for other mutable sequences, which changes them only through
29
+ their own methods: list subclasses that override `__setitem__` or
30
+ `__delitem__`, `collections.UserList`, `collections.deque`, `array.array`,
31
+ `bytearray` and any other `collections.abc.MutableSequence`. Other types
32
+ raise `TypeError`, as do a wrong number of arguments and keyword arguments.
33
+ - Pure-Python implementation `erase._pure` with the same behaviour, used on
34
+ PyPy and other interpreters, where no binary wheel fits, and when the
35
+ environment variable `ERASE_PURE=1` is set.
36
+ - `erase.HAS_C_EXTENSION` and `erase.__version__`.
37
+ - Type information (`py.typed`).
38
+ - Binary wheels for CPython 3.9 to 3.15 and free-threaded 3.14t and 3.15t, a
39
+ pure-Python `py3-none-any` wheel for everything else, and an sdist.
40
+ - `benchmarks/bench.py`, which compares `erase` with list comprehensions.
41
+
42
+ [Unreleased]: https://github.com/KripKrop72724/erase/compare/v0.1.0...HEAD
43
+ [0.1.0]: https://github.com/KripKrop72724/erase/releases/tag/v0.1.0
erase-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Muhammad Asad Janjua
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,9 @@
1
+ # Files the sdist needs on top of setuptools' defaults (pyproject.toml,
2
+ # setup.py, the package sources, py.typed / *.pyi, README.md and LICENSE).
3
+ # csrc/ is grafted explicitly so the C source is in the sdist even when the
4
+ # sdist is built with ERASE_PURE_BUILD=1 (no ext_modules declared).
5
+ include README.md CHANGELOG.md LICENSE
6
+ graft csrc
7
+ graft tests
8
+ graft benchmarks
9
+ global-exclude *.py[cod] *.so *.pyd *.o *.obj .DS_Store
erase-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,171 @@
1
+ Metadata-Version: 2.4
2
+ Name: erase
3
+ Version: 0.1.0
4
+ Summary: Remove every occurrence of a value from a list, in place, with a fast C implementation.
5
+ Author: Muhammad Asad Janjua
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/KripKrop72724/erase
8
+ Project-URL: Source, https://github.com/KripKrop72724/erase
9
+ Project-URL: Issues, https://github.com/KripKrop72724/erase/issues
10
+ Project-URL: Changelog, https://github.com/KripKrop72724/erase/blob/main/CHANGELOG.md
11
+ Keywords: list,remove,remove-all,erase,delete,in-place,sequence,c-extension
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: Operating System :: OS Independent
15
+ Classifier: Programming Language :: C
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3 :: Only
18
+ Classifier: Programming Language :: Python :: 3.9
19
+ Classifier: Programming Language :: Python :: 3.10
20
+ Classifier: Programming Language :: Python :: 3.11
21
+ Classifier: Programming Language :: Python :: 3.12
22
+ Classifier: Programming Language :: Python :: 3.13
23
+ Classifier: Programming Language :: Python :: 3.14
24
+ Classifier: Programming Language :: Python :: 3.15
25
+ Classifier: Programming Language :: Python :: Free Threading :: 2 - Beta
26
+ Classifier: Programming Language :: Python :: Implementation :: CPython
27
+ Classifier: Programming Language :: Python :: Implementation :: PyPy
28
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
29
+ Classifier: Typing :: Typed
30
+ Requires-Python: >=3.9
31
+ Description-Content-Type: text/markdown
32
+ License-File: LICENSE
33
+ Dynamic: license-file
34
+
35
+ # erase
36
+
37
+ [![PyPI version](https://img.shields.io/pypi/v/erase)](https://pypi.org/project/erase/)
38
+
39
+ Remove every occurrence of a value from a list in place, in linear time, and get back how many were removed.
40
+
41
+ ## Install
42
+
43
+ ```
44
+ pip install erase
45
+ ```
46
+
47
+ Requires Python 3.9 or newer. Binary wheels cover CPython 3.9 to 3.15, plus free-threaded 3.14t and 3.15t, on Linux, macOS and Windows. Everywhere else (PyPy, GraalPy, other platforms) pip installs the pure-Python wheel, which gives the same results.
48
+
49
+ ## Usage
50
+
51
+ ```pycon
52
+ >>> from erase import erase
53
+ >>> a = ['x', 'y', 'z', 'x', 'x']
54
+ >>> erase(a, 'x')
55
+ 3
56
+ >>> a
57
+ ['y', 'z']
58
+ >>> erase(a, 'w') # nothing to remove: returns 0, no ValueError
59
+ 0
60
+ ```
61
+
62
+ `erase(seq, value, /)` removes every item equal to `value` from `seq`, in place, and returns how many items it removed. `seq` stays the same object, so every reference to it sees the change. Items and values can be of any type:
63
+
64
+ ```pycon
65
+ >>> row = ['id', None, 'name', None]
66
+ >>> erase(row, None)
67
+ 2
68
+ >>> row
69
+ ['id', 'name']
70
+ >>> nums = [1, 1.0, 2, True, 3]
71
+ >>> erase(nums, 1) # 1 == 1.0 == True, so all three go, as with list.remove
72
+ 3
73
+ >>> nums
74
+ [2, 3]
75
+ >>> import math
76
+ >>> data = [0.5, math.nan, 1.5, math.nan]
77
+ >>> erase(data, math.nan) # the very object passed is removed, as with list.remove
78
+ 2
79
+ >>> data
80
+ [0.5, 1.5]
81
+ ```
82
+
83
+ ## Why not a one-liner?
84
+
85
+ | Idiom | Pitfall |
86
+ |---|---|
87
+ | `a = [x for x in a if x != v]` | Builds a new list and rebinds the name `a`. Every other reference to the list (the caller's variable, an attribute) still sees the old contents. |
88
+ | `a[:] = [x for x in a if x != v]` | In place, but first builds a temporary list of all kept items. Unlike `list.remove`, it does not check identity first, so if `v` is a NaN object, the items that are that very object are kept. |
89
+ | `list(filter((2).__ne__, a))`, the first suggestion in the [accepted Stack Overflow answer](https://stackoverflow.com/a/1157160) | Builds a new list, and mishandles equal values of other numeric types: removing `1` keeps `1.0`, because `(1).__ne__(1.0)` returns `NotImplemented`, which counts as true. On Python 3.14+ it raises `TypeError` at the first item that is not an `int`. |
90
+ | `while v in a: a.remove(v)` | Correct, but every removal searches again from the start and shifts the rest of the list: O(n·k) for k matches. At 32,000 items with 10% matches it is about 1,000 times slower than a comprehension. |
91
+ | `a.remove(x)` inside `for x in a:` | Changes the list while iterating over it, so the item after each removed one is skipped: `[1, 1, 2]` becomes `[1, 2]`. |
92
+ | `a[:] = [x for x in a if not (x is v or x == v)]` | Same result as `erase` (same test), but builds a temporary list and is usually slower than the `!=` version (see Performance). |
93
+
94
+ ## Semantics
95
+
96
+ - **Same test as `list.remove`.** An item `x` is removed when `x is value or x == value`, with the item on the left of `==` and the result's truth value used. `list.remove`, `list.count` and `in` use the same test. Each item is compared exactly once, in index order. If the comparisons have no side effects, `erase(a, v)` returns what `a.count(v)` returned before the call and leaves `a` as `while v in a: a.remove(v)` would.
97
+ - **In place.** The sequence is changed, not replaced. Kept items keep their order.
98
+ - **All or nothing.** If a comparison raises, or testing its result for truth raises, the exception propagates and the sequence is left unchanged.
99
+ - **Safe if the sequence changes during the comparisons.** Code that runs while `erase` compares (an `__eq__` method, a `__del__` it triggers, another thread) may change the sequence. If its length changed, or a slot about to be removed no longer holds the object that compared equal, `erase` raises `RuntimeError("list modified during erase()")` and changes nothing. Changes that pass this check, such as an item replaced at a kept position, are preserved. `erase` never removes an item that did not compare equal (for threads, see the FAQ).
100
+ - **Memory is given back.** When most items of a list are removed, its spare capacity is released, as `del` and `list.pop()` release it (on free-threaded builds, at the list's next resize).
101
+
102
+ ## Performance
103
+
104
+ Apple M2, CPython 3.13. Measured with [`benchmarks/bench.py`](https://github.com/KripKrop72724/erase/blob/main/benchmarks/bench.py): minimum of 15 interleaved runs, each on a fresh copy of the list.
105
+
106
+ | Scenario | `erase` | `!=` comprehension | identity-aware comprehension |
107
+ |---|--:|--:|--:|
108
+ | 1,000,000 str, 0.1% match | 2.20 ms | 17.9 ms (8.2x) | 24.8 ms (11.3x) |
109
+ | 1,000,000 str, 10% match | 2.93 ms | 19.2 ms (6.5x) | 26.4 ms (9.0x) |
110
+ | 1,000,000 small int, 10% match | 1.38 ms | 14.9 ms (10.8x) | 22.3 ms (16.1x) |
111
+ | 1,000,000 str and int, 10% None | 1.88 ms | 32.2 ms (17.2x) | 36.4 ms (19.4x) |
112
+ | 100,000 objects with a Python `__eq__`, 10% match | 4.38 ms | 7.78 ms (1.8x) | 5.76 ms (1.3x) |
113
+ | 10-item list, 2 matches | 43.1 ns | 218 ns (5.1x) | 310 ns (7.2x) |
114
+
115
+ `!=` comprehension: `a[:] = [x for x in a if x != v]`. Identity-aware comprehension: `a[:] = [x for x in a if not (x is v or x == v)]`, the same test as `erase`. In parentheses: that time divided by the time of `erase`.
116
+
117
+ **Memory.** The C extension builds no temporary list. On the 1,000,000 strings its peak extra memory is 0.13 MB with 0.1% matches and 0.93 MB with 10% (one bit per item plus one pointer per removed item), against 16.4 MB and 15.5 MB for the `!=` comprehension (the temporary list of kept items, plus a buffer for the old items that the slice assignment allocates).
118
+
119
+ **Where it gains little.** For objects whose `__eq__` is written in Python, almost all of the time goes into running `__eq__`, which `erase` has to call just like a comprehension does, so it is only a little faster than the identity-aware comprehension, which also uses `==`. The `!=` comprehension is slower than that one because, for a class that defines only `__eq__`, `!=` goes through the inherited `__ne__`, which calls `__eq__` and inverts the result.
120
+
121
+ The pure-Python implementation has the same semantics but is not faster than a comprehension.
122
+
123
+ ## Supported types
124
+
125
+ - **`list`**, and subclasses of `list` that do not override `__setitem__` or `__delitem__`: the fast path, which with the C extension works directly on the list's storage.
126
+ - **Other mutable sequences**: list subclasses that override those methods, `collections.UserList`, `collections.deque`, `array.array`, `bytearray`, and any other `collections.abc.MutableSequence`. They take a generic path that changes the object only through its own methods, with the same test and the same checks.
127
+ - **Anything else** (`tuple`, `str`, `bytes`, `range`, `dict`, `set`, `None`, ...) raises `TypeError`.
128
+
129
+ ```pycon
130
+ >>> from collections import deque
131
+ >>> d = deque([1, 2, 1, 3])
132
+ >>> erase(d, 1)
133
+ 2
134
+ >>> d
135
+ deque([2, 3])
136
+ >>> buf = bytearray(b'a,b,,c')
137
+ >>> erase(buf, ord(','))
138
+ 3
139
+ >>> buf
140
+ bytearray(b'abc')
141
+ >>> erase((1, 2, 1), 1)
142
+ Traceback (most recent call last):
143
+ ...
144
+ TypeError: erase() argument 1 must be a mutable sequence, not tuple
145
+ ```
146
+
147
+ ## Implementation notes
148
+
149
+ - **C extension.** For lists, `erase._erase` makes one O(n) pass that compares every item once and marks the matches in a bitmap, then compacts the list in place: kept items slide down with plain pointer moves, the length is set, and only then are the removed items released, so any `__del__` they trigger sees a consistent list.
150
+ - **Fast comparisons.** Items and values of the exact types `str`, `int`, `float` and `bytes` are compared directly in C, and built-in types that can never be equal, such as `None` and `str`, are not compared at all. Everything else goes through the normal `==`.
151
+ - **Pure-Python fallback.** `erase._pure` implements the same semantics. It is used on PyPy, GraalPy and wherever no binary wheel fits, and when the extension cannot be compiled from the source distribution.
152
+ - **Which one is active.** `erase.HAS_C_EXTENSION` is `True` when the C extension is in use. Set the environment variable `ERASE_PURE=1` before importing `erase` to use the pure-Python implementation anyway, for example to compare the two. `erase.__version__` gives the version.
153
+ - **Free-threaded CPython.** Wheels for 3.14t and 3.15t. The extension does not need the GIL; it locks the list with a per-object critical section, as list methods do. On free-threaded builds a list's spare capacity is released at its next resize instead of during `erase`.
154
+
155
+ ## FAQ
156
+
157
+ **Why isn't this a list method?**
158
+ Built-in types cannot be extended from Python: `list.erase = ...` raises `TypeError`. CPython core developers declined `list.remove_all()`, `list.removeall()` and a count argument for `list.remove()` several times between 2013 and 2026, and said that such a single-pass variant belongs in an extension. This is that extension.
159
+
160
+ **Is it thread-safe?**
161
+ With the C extension, on a list, yes: the comparisons, the check and the compaction run under the GIL, or under the list's lock on free-threaded builds. Another thread can only change the list while a comparison runs Python code, such as an `__eq__` written in Python, and such changes are handled as described under Semantics: `erase` never corrupts the list or removes an item that did not compare equal, and raises `RuntimeError` rather than guess. The pure-Python implementation and the generic path for other sequences make the same check, but another thread can still change the sequence between the check and the write-back, so protect a sequence shared between threads with a lock, as for any read-modify-write.
162
+
163
+ **Why did `erase(a, 1)` also remove `1.0` and `True`?**
164
+ Because they are equal to `1`: `1 == 1.0 == True`. `erase` uses the same test as `list.remove`, `list.count` and `in`. To remove only ints, use a comprehension with a type check.
165
+
166
+ **Can it remove every NaN?**
167
+ No. NaN is not equal to anything, itself included, so `erase` removes only the very NaN object you pass, as `list.remove` does; `float('nan')` makes a new object each time. To drop every NaN, use `a[:] = [x for x in a if x == x]`.
168
+
169
+ ## License
170
+
171
+ MIT. See [LICENSE](https://github.com/KripKrop72724/erase/blob/main/LICENSE).
erase-0.1.0/README.md ADDED
@@ -0,0 +1,137 @@
1
+ # erase
2
+
3
+ [![PyPI version](https://img.shields.io/pypi/v/erase)](https://pypi.org/project/erase/)
4
+
5
+ Remove every occurrence of a value from a list in place, in linear time, and get back how many were removed.
6
+
7
+ ## Install
8
+
9
+ ```
10
+ pip install erase
11
+ ```
12
+
13
+ Requires Python 3.9 or newer. Binary wheels cover CPython 3.9 to 3.15, plus free-threaded 3.14t and 3.15t, on Linux, macOS and Windows. Everywhere else (PyPy, GraalPy, other platforms) pip installs the pure-Python wheel, which gives the same results.
14
+
15
+ ## Usage
16
+
17
+ ```pycon
18
+ >>> from erase import erase
19
+ >>> a = ['x', 'y', 'z', 'x', 'x']
20
+ >>> erase(a, 'x')
21
+ 3
22
+ >>> a
23
+ ['y', 'z']
24
+ >>> erase(a, 'w') # nothing to remove: returns 0, no ValueError
25
+ 0
26
+ ```
27
+
28
+ `erase(seq, value, /)` removes every item equal to `value` from `seq`, in place, and returns how many items it removed. `seq` stays the same object, so every reference to it sees the change. Items and values can be of any type:
29
+
30
+ ```pycon
31
+ >>> row = ['id', None, 'name', None]
32
+ >>> erase(row, None)
33
+ 2
34
+ >>> row
35
+ ['id', 'name']
36
+ >>> nums = [1, 1.0, 2, True, 3]
37
+ >>> erase(nums, 1) # 1 == 1.0 == True, so all three go, as with list.remove
38
+ 3
39
+ >>> nums
40
+ [2, 3]
41
+ >>> import math
42
+ >>> data = [0.5, math.nan, 1.5, math.nan]
43
+ >>> erase(data, math.nan) # the very object passed is removed, as with list.remove
44
+ 2
45
+ >>> data
46
+ [0.5, 1.5]
47
+ ```
48
+
49
+ ## Why not a one-liner?
50
+
51
+ | Idiom | Pitfall |
52
+ |---|---|
53
+ | `a = [x for x in a if x != v]` | Builds a new list and rebinds the name `a`. Every other reference to the list (the caller's variable, an attribute) still sees the old contents. |
54
+ | `a[:] = [x for x in a if x != v]` | In place, but first builds a temporary list of all kept items. Unlike `list.remove`, it does not check identity first, so if `v` is a NaN object, the items that are that very object are kept. |
55
+ | `list(filter((2).__ne__, a))`, the first suggestion in the [accepted Stack Overflow answer](https://stackoverflow.com/a/1157160) | Builds a new list, and mishandles equal values of other numeric types: removing `1` keeps `1.0`, because `(1).__ne__(1.0)` returns `NotImplemented`, which counts as true. On Python 3.14+ it raises `TypeError` at the first item that is not an `int`. |
56
+ | `while v in a: a.remove(v)` | Correct, but every removal searches again from the start and shifts the rest of the list: O(n·k) for k matches. At 32,000 items with 10% matches it is about 1,000 times slower than a comprehension. |
57
+ | `a.remove(x)` inside `for x in a:` | Changes the list while iterating over it, so the item after each removed one is skipped: `[1, 1, 2]` becomes `[1, 2]`. |
58
+ | `a[:] = [x for x in a if not (x is v or x == v)]` | Same result as `erase` (same test), but builds a temporary list and is usually slower than the `!=` version (see Performance). |
59
+
60
+ ## Semantics
61
+
62
+ - **Same test as `list.remove`.** An item `x` is removed when `x is value or x == value`, with the item on the left of `==` and the result's truth value used. `list.remove`, `list.count` and `in` use the same test. Each item is compared exactly once, in index order. If the comparisons have no side effects, `erase(a, v)` returns what `a.count(v)` returned before the call and leaves `a` as `while v in a: a.remove(v)` would.
63
+ - **In place.** The sequence is changed, not replaced. Kept items keep their order.
64
+ - **All or nothing.** If a comparison raises, or testing its result for truth raises, the exception propagates and the sequence is left unchanged.
65
+ - **Safe if the sequence changes during the comparisons.** Code that runs while `erase` compares (an `__eq__` method, a `__del__` it triggers, another thread) may change the sequence. If its length changed, or a slot about to be removed no longer holds the object that compared equal, `erase` raises `RuntimeError("list modified during erase()")` and changes nothing. Changes that pass this check, such as an item replaced at a kept position, are preserved. `erase` never removes an item that did not compare equal (for threads, see the FAQ).
66
+ - **Memory is given back.** When most items of a list are removed, its spare capacity is released, as `del` and `list.pop()` release it (on free-threaded builds, at the list's next resize).
67
+
68
+ ## Performance
69
+
70
+ Apple M2, CPython 3.13. Measured with [`benchmarks/bench.py`](https://github.com/KripKrop72724/erase/blob/main/benchmarks/bench.py): minimum of 15 interleaved runs, each on a fresh copy of the list.
71
+
72
+ | Scenario | `erase` | `!=` comprehension | identity-aware comprehension |
73
+ |---|--:|--:|--:|
74
+ | 1,000,000 str, 0.1% match | 2.20 ms | 17.9 ms (8.2x) | 24.8 ms (11.3x) |
75
+ | 1,000,000 str, 10% match | 2.93 ms | 19.2 ms (6.5x) | 26.4 ms (9.0x) |
76
+ | 1,000,000 small int, 10% match | 1.38 ms | 14.9 ms (10.8x) | 22.3 ms (16.1x) |
77
+ | 1,000,000 str and int, 10% None | 1.88 ms | 32.2 ms (17.2x) | 36.4 ms (19.4x) |
78
+ | 100,000 objects with a Python `__eq__`, 10% match | 4.38 ms | 7.78 ms (1.8x) | 5.76 ms (1.3x) |
79
+ | 10-item list, 2 matches | 43.1 ns | 218 ns (5.1x) | 310 ns (7.2x) |
80
+
81
+ `!=` comprehension: `a[:] = [x for x in a if x != v]`. Identity-aware comprehension: `a[:] = [x for x in a if not (x is v or x == v)]`, the same test as `erase`. In parentheses: that time divided by the time of `erase`.
82
+
83
+ **Memory.** The C extension builds no temporary list. On the 1,000,000 strings its peak extra memory is 0.13 MB with 0.1% matches and 0.93 MB with 10% (one bit per item plus one pointer per removed item), against 16.4 MB and 15.5 MB for the `!=` comprehension (the temporary list of kept items, plus a buffer for the old items that the slice assignment allocates).
84
+
85
+ **Where it gains little.** For objects whose `__eq__` is written in Python, almost all of the time goes into running `__eq__`, which `erase` has to call just like a comprehension does, so it is only a little faster than the identity-aware comprehension, which also uses `==`. The `!=` comprehension is slower than that one because, for a class that defines only `__eq__`, `!=` goes through the inherited `__ne__`, which calls `__eq__` and inverts the result.
86
+
87
+ The pure-Python implementation has the same semantics but is not faster than a comprehension.
88
+
89
+ ## Supported types
90
+
91
+ - **`list`**, and subclasses of `list` that do not override `__setitem__` or `__delitem__`: the fast path, which with the C extension works directly on the list's storage.
92
+ - **Other mutable sequences**: list subclasses that override those methods, `collections.UserList`, `collections.deque`, `array.array`, `bytearray`, and any other `collections.abc.MutableSequence`. They take a generic path that changes the object only through its own methods, with the same test and the same checks.
93
+ - **Anything else** (`tuple`, `str`, `bytes`, `range`, `dict`, `set`, `None`, ...) raises `TypeError`.
94
+
95
+ ```pycon
96
+ >>> from collections import deque
97
+ >>> d = deque([1, 2, 1, 3])
98
+ >>> erase(d, 1)
99
+ 2
100
+ >>> d
101
+ deque([2, 3])
102
+ >>> buf = bytearray(b'a,b,,c')
103
+ >>> erase(buf, ord(','))
104
+ 3
105
+ >>> buf
106
+ bytearray(b'abc')
107
+ >>> erase((1, 2, 1), 1)
108
+ Traceback (most recent call last):
109
+ ...
110
+ TypeError: erase() argument 1 must be a mutable sequence, not tuple
111
+ ```
112
+
113
+ ## Implementation notes
114
+
115
+ - **C extension.** For lists, `erase._erase` makes one O(n) pass that compares every item once and marks the matches in a bitmap, then compacts the list in place: kept items slide down with plain pointer moves, the length is set, and only then are the removed items released, so any `__del__` they trigger sees a consistent list.
116
+ - **Fast comparisons.** Items and values of the exact types `str`, `int`, `float` and `bytes` are compared directly in C, and built-in types that can never be equal, such as `None` and `str`, are not compared at all. Everything else goes through the normal `==`.
117
+ - **Pure-Python fallback.** `erase._pure` implements the same semantics. It is used on PyPy, GraalPy and wherever no binary wheel fits, and when the extension cannot be compiled from the source distribution.
118
+ - **Which one is active.** `erase.HAS_C_EXTENSION` is `True` when the C extension is in use. Set the environment variable `ERASE_PURE=1` before importing `erase` to use the pure-Python implementation anyway, for example to compare the two. `erase.__version__` gives the version.
119
+ - **Free-threaded CPython.** Wheels for 3.14t and 3.15t. The extension does not need the GIL; it locks the list with a per-object critical section, as list methods do. On free-threaded builds a list's spare capacity is released at its next resize instead of during `erase`.
120
+
121
+ ## FAQ
122
+
123
+ **Why isn't this a list method?**
124
+ Built-in types cannot be extended from Python: `list.erase = ...` raises `TypeError`. CPython core developers declined `list.remove_all()`, `list.removeall()` and a count argument for `list.remove()` several times between 2013 and 2026, and said that such a single-pass variant belongs in an extension. This is that extension.
125
+
126
+ **Is it thread-safe?**
127
+ With the C extension, on a list, yes: the comparisons, the check and the compaction run under the GIL, or under the list's lock on free-threaded builds. Another thread can only change the list while a comparison runs Python code, such as an `__eq__` written in Python, and such changes are handled as described under Semantics: `erase` never corrupts the list or removes an item that did not compare equal, and raises `RuntimeError` rather than guess. The pure-Python implementation and the generic path for other sequences make the same check, but another thread can still change the sequence between the check and the write-back, so protect a sequence shared between threads with a lock, as for any read-modify-write.
128
+
129
+ **Why did `erase(a, 1)` also remove `1.0` and `True`?**
130
+ Because they are equal to `1`: `1 == 1.0 == True`. `erase` uses the same test as `list.remove`, `list.count` and `in`. To remove only ints, use a comprehension with a type check.
131
+
132
+ **Can it remove every NaN?**
133
+ No. NaN is not equal to anything, itself included, so `erase` removes only the very NaN object you pass, as `list.remove` does; `float('nan')` makes a new object each time. To drop every NaN, use `a[:] = [x for x in a if x == x]`.
134
+
135
+ ## License
136
+
137
+ MIT. See [LICENSE](https://github.com/KripKrop72724/erase/blob/main/LICENSE).
@@ -0,0 +1,39 @@
1
+ # Benchmarks
2
+
3
+ `bench.py` times `erase(a, v)` against the two list comprehensions it replaces:
4
+
5
+ - `a[:] = [x for x in a if x != v]`, the usual idiom;
6
+ - `a[:] = [x for x in a if not (x is v or x == v)]`, which uses the same test as `erase`.
7
+
8
+ It needs only the standard library and an installed `erase`:
9
+
10
+ ```
11
+ pip install erase # or: pip install . (in the repository root)
12
+ python benchmarks/bench.py # 1,000,000-item lists, 15 rounds
13
+ python benchmarks/bench.py --quick
14
+ python benchmarks/bench.py --help
15
+ ```
16
+
17
+ Scenarios (`--size` sets the 1,000,000):
18
+
19
+ - 1,000,000 strings with 0.1% and with 10% matches; every item is its own `str`
20
+ object, and the value equals the matches without being the same object;
21
+ - 1,000,000 small ints with 10% matches;
22
+ - 1,000,000 strings and ints with 10% `None`, removing `None`;
23
+ - 100,000 instances of a class whose `__eq__` is written in Python, 10% matches;
24
+ - a 10-item list of strings with 2 matches.
25
+
26
+ Every timed call gets a fresh copy of the list (the copy is not timed). The
27
+ statements run interleaved, in a new random order each round, with the garbage
28
+ collector off, and the minimum over all rounds is reported. The 10-item case
29
+ times a loop of copies and calls and subtracts the same loop without the call.
30
+ Before timing, each scenario checks that all three statements leave exactly the
31
+ same objects in the same order, and that `erase` returns the number removed.
32
+
33
+ The output starts with the `erase` version, whether the C extension or the
34
+ pure-Python implementation was used (set `ERASE_PURE=1` to time the latter),
35
+ the Python version and the CPU. Then come two Markdown tables: the times, in the
36
+ format of the table in the Performance section of the main README, and the peak
37
+ extra memory of one call, measured with `tracemalloc`, which the memory note
38
+ there quotes. On a busy machine, `--cpu-time` measures the thread's CPU time
39
+ instead of wall time.