Bytearray functions in Python (2)

Buy Me a Coffee☕

*Memo:

My post explains bytearray functions (1).

My post explains string, bytes and bytearray functions.

remove() can remove the 1st byte matched to value from the bytearray, searching from the left to the right as shown below:


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)

Buy Me a Coffee

*Memo:

  • My post explains bytearray functions (1).
  • My post explains string, bytes and bytearray functions.

remove() can remove the 1st byte matched to value from the bytearray, searching from the left to the right as shown below:

*Memo:

  • The 1st argument is value(Required-Type:int):
    • Don't use value=.
  • Error occurs if value doesn't exist.
v = bytearray(b'ABCAB')

v.remove(ord('B'))
# v.remove(66)

print(v)
# bytearray(b'ACAB')

v.remove(ord('B'))
# v.remove(66)

print(v)
# bytearray(b'ACA')

v.remove(ord('C'))
# v.remove(67)

print(v)
# bytearray(b'AA')

v.remove(ord('a'))
# v.remove(97)
# ValueError: value not found in bytearray

pop() can remove and throw the byte from index in the bytearray in the range [The 1st index, The last index] as shown below:

*Memo:

  • The 1st argument is index(Optional-Default:-1):
    • -1 means the last index.
    • Don't use index=.
  • index can be signed indices(zero and positive and negative indices).
  • Error occurs if index is out of range.
v = bytearray(b'ABCAB')

print(v.pop())     # 66
# print(v.pop(4))  # 66
# print(v.pop(-1)) # 66

print(v)
# bytearray(b'ABCA')

print(v.pop(1))    # 66
# print(v.pop(-3)) # 66

print(v)
# bytearray(b'ACA')

print(v.pop(1))    # 66
# print(v.pop(-2)) # 66

print(v)
# bytearray(b'AA')

print(v.pop(-3))
print(v.pop(2))
# IndexError: pop index out of range

clear() can remove all bytes from the bytearray as shown below:

*Memo:

v = bytearray(b'ABCDE')

v.clear()
# del v[:]

print(v)
# bytearray(b'')

reverse() can reverse the bytearray as shown below:

*Memo:

  • It has no arguments.
v = bytearray(b'ABCDE')

v.reverse()

print(v)
# bytearray(b'EDCBA')


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)


Print Share Comment Cite Upload Translate Updates
APA

Super Kai (Kazuya Ito) | Sciencx (2025-10-20T08:27:03+00:00) Bytearray functions in Python (2). Retrieved from https://www.scien.cx/2025/10/20/bytearray-functions-in-python-2/

MLA
" » Bytearray functions in Python (2)." Super Kai (Kazuya Ito) | Sciencx - Monday October 20, 2025, https://www.scien.cx/2025/10/20/bytearray-functions-in-python-2/
HARVARD
Super Kai (Kazuya Ito) | Sciencx Monday October 20, 2025 » Bytearray functions in Python (2)., viewed ,<https://www.scien.cx/2025/10/20/bytearray-functions-in-python-2/>
VANCOUVER
Super Kai (Kazuya Ito) | Sciencx - » Bytearray functions in Python (2). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/10/20/bytearray-functions-in-python-2/
CHICAGO
" » Bytearray functions in Python (2)." Super Kai (Kazuya Ito) | Sciencx - Accessed . https://www.scien.cx/2025/10/20/bytearray-functions-in-python-2/
IEEE
" » Bytearray functions in Python (2)." Super Kai (Kazuya Ito) | Sciencx [Online]. Available: https://www.scien.cx/2025/10/20/bytearray-functions-in-python-2/. [Accessed: ]
rf:citation
» Bytearray functions in Python (2) | Super Kai (Kazuya Ito) | Sciencx | https://www.scien.cx/2025/10/20/bytearray-functions-in-python-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.