*Memo:
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=.
- Don't use
- Error occurs if
valuedoesn'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):-
-1means the last index. - Don't use
index=.
-
-
indexcan be signed indices(zero and positive and negative indices). - Error occurs if
indexis 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:
- It has no arguments.
-
del ...[:] can do
clear().
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')
Top comments (0)