bytearray.remove()

Removes by VALUE, and only the first match. The value-versus-position distinction is the one that catches people, since pop takes a position and looks similar.

Bytearray methodPython 3.0+Live demo
Common call
buf.remove(0x00)
Returns
None — one byte is deleted
Replaces
del buf[buf.index(byte)]
Watch out
removes only the FIRST match, and raises when absent
bytearray.remove(bytebyte — The byte VALUE to remove, as an integer from 0 to 255. Not a position.type: int · required)
→ None

Demo

Live evaluation
Try:
Inputs
sstrbuffer (as text)
byteintbyte value to remove
Output
[b := bytearray(bytes('abc', 'utf-8')), b.remove(98), b][2]
bytearray(b'ac')

The argument is a byte VALUE, so 98 removes the letter b. With duplicates only the FIRST is removed — the second case leaves the later a in place. A value that is not present raises ValueError rather than doing nothing, which is why the last two cases fail: neither 122 nor 0 appears in "abc".

Parameters

NameTypeRequiredDescription
byteintyesThe byte VALUE to remove, as an integer from 0 to 255. Not a position.

Return value

None — Returns None — the buffer shrinks in place. Raises ValueError if the byte is not present.

Common patterns

Strip a specific byte once
When exactly one occurrence is expected.
buf.remove(0x00)
Remove safely
The membership test avoids the exception.
if byte in buf:
    buf.remove(byte)
Remove every occurrence
remove handles one at a time; a loop or a filter handles all.
while byte in buf:
    buf.remove(byte)

Examples

1. By value
b = bytearray(b'abc') b.remove(98) b
Returns
bytearray(b'ac')
2. First of two
b = bytearray(b'abcabc') b.remove(97) b
Returns
bytearray(b'bcabc')
3. Absent raises
bytearray(b'abc').remove(122)
Returns
ValueError: value not found in bytearray
4. Returns None
bytearray(b'abc').remove(97)
Returns
None
5. Not a position
b = bytearray(b'abc') b.remove(0)
Returns
ValueError — no NUL byte present
6. Use ord
buf.remove(ord('b'))
Returns
removes b

Pitfalls

1. It takes a value, not an index
remove(0) deletes the byte whose VALUE is zero, not the byte at position zero. Confusing it with pop is easy and the failure is loud but baffling — a ValueError on a buffer that obviously has a first byte.
Read as a position
bytearray(b'abc').remove(0)
ValueError: value not found in bytearray
pop takes positions
bytearray(b'abc').pop(0)
97
2. Only the first match goes
Duplicates survive. Code assuming remove clears every occurrence leaves the rest behind silently.
Others remain
b = bytearray(b'aaa')
b.remove(97)
b
bytearray(b'aa')
Loop or filter
while 97 in b:
    b.remove(97)
bytearray(b'')
3. Absence raises rather than being ignored
There is no quiet no-op. Removing an optional byte needs a membership test or a try block.
Uncaught
buf.remove(byte)
ValueError: value not found in bytearray
Test first
if byte in buf:
    buf.remove(byte)
safe
4. Removing while iterating skips bytes
Mutating a buffer during a loop over it shifts the remaining bytes under the iterator, so some are silently never visited.
Skips entries
for v in buf:
    if v == 0:
        buf.remove(v)
misses some zeros
Rebuild instead
buf = bytearray(v for v in buf if v != 0)
all removed

When to use

Use it
  • Deleting a single known byte value
  • Stripping one marker where exactly one is expected
  • Small buffers where the shift cost does not matter
Reach for something else
  • Removing by position → pop or del
  • Removing every occurrence → rebuild with a comprehension
  • Large buffers → rebuilding once beats repeated shifting

Notes

Complexity
O(n) — a scan to find the byte, then a shift to close the gap
Return
None; the bytearray is mutated in place
CPython impl
Objects/bytearrayobject.c :: bytearray_remove
Memory
No allocation; the internal buffer may shrink
Thread-safe
Not safe under concurrent mutation of the same buffer

FAQ

remove deletes one match per call. Loop while the value is still present, or rebuild the buffer with a comprehension — the rebuild is a single pass and far better on large data.

buf = bytearray(v for v in buf if v != target)

History

3.0
bytearray introduced as the mutable counterpart to bytes.