The pop() method of bytearray in Python

The pop() method removes an element from the bytearray. When the index is not specified the last element is removed. When the index is specified, it removes the element from the given index.

Bytearray Updated 8 Jan 2026 1 min read

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

python
# 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

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')