The pop() method of bytearray in Python

Overview:

  • The pop() method of bytearray in Python removes an element from the last position. By default, the index parameter assumes the value of -1 which denotes the last position.
  • If a value is passed to the parameter index, then the element from the given index is removed.

Example:

# Example Python program that removes an element from
# bytearray using the pop() method
line = bytearray(b"A sailor went to sea, sea, sea")

# Remove a byte from the last position
line.pop()
print("After pop() removing last element:")
print(line)

# Remove two bytes continously from the first position
print("After pop() removing first two elements:")
line.pop(0)
line.pop(0)
print(line)

Output:

After pop() removing last element:
bytearray(b'A sailor went to sea, sea, se')
After pop() removing first two elements:
bytearray(b'sailor went to sea, sea, se')

 


Copyright 2026 © pythontic.com