Python Glossary Русский

all Python 3Byte sequencesfunction

bytearray.pop

bytearray — mutating

Remove and return a byte at an index (last by default).

Syntax

ba.pop(...)

The value comes back as an int in 0-255, not as a one-byte sequence, so comparing it to b'a' never matches — compare with 97 or ord('a') instead. Popping from the end is cheap, while pop(0) or a pop from the middle shifts the whole tail and costs O(n); an empty bytearray or an out-of-range index raises IndexError.

Examples

ba = bytearray(b'abc')
print(ba.pop())   # → 99
print(bytearray(b'abc').pop(0))   # → 97
print(bytearray(b'abc').pop(-2))   # → 98
ba2 = bytearray(b'abc'); ba2.pop(); print(ba2)   # → bytearray(b'ab')
print(bytearray().pop())   # → IndexError

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy