The remove() method of bytearray class in Python

Overview:

  • The remove() method accepts an integer value in the range (0, 255) and removes the corresponding element from the bytearray.

Example:

# Example Python program that removes an 
# element whose value matches a given value
# using the remove() method

firstLine = bytearray(b"Hey diddle, diddle!")
print("Original bytearray:")
print(firstLine)

# Remove an element by providing the ASCII value 
firstLine.remove(ord(b"!"))
print("The new bytearray:")
print(firstLine)

# Remove an element whose ASCII value matches
# the character "," 
firstLine.remove(ord(b","))
print("Bytearray after another removal of an element:")
print(firstLine)

Output:

The new bytearray:
bytearray(b'Hey diddle, diddle')
Bytearray after another removal of an element:
bytearray(b'Hey diddle diddle')

 


Copyright 2026 © pythontic.com