Overview:
- The del operator in Python, when applied on a bytearray object removes one or more elements from the bytearray.
- The bytearray is a mutable sequence of bytes. The removal of one or more elements using the del operator is one of the operations that makes the bytearray a mutable sequence. The other operations include replace and extend performed through the operators [], += and *=.
Example - Remove an element from a bytearray:
|
# Example Python program that removes an # Create a bytearray # Remove a byte given by an index |
Output:
| The bytearray: bytearray(b'School one day, school one day') The bytearray after removing an element at position 14: bytearray(b'School one day school one day') |
Example - Remove a slice of elements from a bytearray:
|
# Example Python program that removes # Create a bytearray # Remove a slice from the bytearray del rhymeBytes[sliceBegin:sliceEnd] print("The bytearray after removing a slice:") |
Output:
| Original bytearray: bytearray(b'Morning bells are ringing!') The bytearray after removing a slice: bytearray(b'Morning bells ringing!') |