The del operator of bytearray class in Python

In Python, the del operator removes one or more elements from a bytearray as specified through a single index or a start index and end index. A bytearray is a mutable sequence of bytes.

Bytearray Updated 7 Jan 2026 1 min read

Overview

  • The del operator in Python, when applied on a bytearray object removes one or more elements from the bytearray.
  • The bytearray is a mutable sequence of bytes. The removal of one or more elements using the del operator is one of the operations that makes the bytearray a mutable sequence. The other operations include replace and extend performed through the operators [], += and *=.

Example - Remove an element from a bytearray

python
# Example Python program that removes an

            # element from a given index

# Create a bytearray

            songBytes = bytearray(b"School one day, school one day")

            print("The bytearray:")

            print(songBytes)

# Remove a byte given by an index

            index = 14

            del songBytes[index]

            print("The bytearray after removing an element at position {}:".format(index))

            print(songBytes)

Output

Output
The bytearray:

            bytearray(b'School one day, school one day')

            The bytearray after removing an element at position 14:

            bytearray(b'School one day school one day')

Example - Remove a slice of elements from a bytearray

python
# Example Python program that removes

            # a slice of elements from a bytearray

# Create a bytearray

            rhymeBytes = bytearray(b"Morning bells are ringing!")

            print("Original bytearray:")

            print(rhymeBytes)

# Remove a slice from the bytearray

            sliceBegin     = 13

            sliceEnd     = 17

del rhymeBytes[sliceBegin:sliceEnd]

print("The bytearray after removing a slice:")

            print(rhymeBytes)

Output

Output
Original bytearray:

            bytearray(b'Morning bells are ringing!')

            The bytearray after removing a slice:

            bytearray(b'Morning bells ringing!')