Bytearray Methods

bytearray.copy()

Return a new bytearray containing the current byte values. Unlike alias assignment, modifying either buffer does not alter the other. Although mutable sequence copying is often described as shallow, bytearray elements are integer values, not nested mutable references. Copying does not resize the source and is allowed during active memoryview exports. The result is a base bytearray in CPython 3.13.5, including for subclass receivers.

Formal Syntax & Structure

python syntaxpython
data.copy()

Return Value

bytearray

New mutable bytearray containing independent storage with the same byte values.

Working Example

Example Snippetpython
original = bytearray(b"ABC")clone = original.copy()clone[0] = 90clone.append(33)print(original)print(clone)print(original is clone)
Python Reference