Bytearray Methods

bytearray.insert()

Insert one integer byte before the given position, shifting following bytes right. Ordinary indices outside the sequence are clamped to its ends, and negative indices are interpreted relative to the end. Very large indices outside the platform index range raise OverflowError on CPython 3.13.5. Byte values outside 0–255 raise ValueError. Growth is forbidden while the buffer is exported.

Formal Syntax & Structure

python syntaxpython
data.insert(index, item, /)

Parameters & Attributes

NameTypeDescription
indexint | SupportsIndexPosition interpreted using integer indexing.
itemint | SupportsIndexOne integer byte value from 0 through 255; not a one-byte bytes object.

Return Value

None

None; modifies the existing buffer.

Working Example

Example Snippetpython
data = bytearray(b"AC")print(data.insert(1, 66))data.insert(-1, 88)data.insert(99, 33)print(data)
Python Reference