DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

Bytearray in Python (2)

Buy Me a Coffee

*Memo:

A bytearray can be read by indexing or slicing as shown below:

*Memo:

  • Indexing can be done with one or more [index].
  • Slicing can be done with one or more [start:end:step]:
    • start(Optional-Default:The index of the 1st element):
      • It's a start index(inclusive).
    • end(Optional-Default:The index of the last element + 1):
      • It's an end index(exclusive).
    • step(Optional-Default:1):
      • It's the interval of indices.
      • It cannot be zero.
    • The [] with at least one : is slicing.
v = bytearray(b'ABCDEFGH')

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

print(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7])
print(v[-8], v[-7], v[-6], v[-5], v[-4], v[-3], v[-2], v[-1])
# 65 66 67 68 69 70 71 72
Enter fullscreen mode Exit fullscreen mode
v = bytearray(b'ABCDEFGH')

print(v[:])
print(v[::])
# bytearray(b'ABCDEFGH')

print(v[::2])
# bytearray(b'ACEG')

print(v[::-2])
# bytearray(b'HFDB')

print(v[2:])
print(v[-6:])
print(v[2::])
print(v[-6::])
# bytearray(b'CDEFGH')

print(v[2::2])
print(v[-6::2])
# bytearray(b'CEG')

print(v[2::-2])
print(v[-6::-2])
# bytearray(b'CA')

print(v[:6])
print(v[:-2])
print(v[:6:])
print(v[:-2:])
# bytearray(b'ABCDEF')

print(v[:6:2])
print(v[:-2:2])
# bytearray(b'ACE')

print(v[:6:-2])
print(v[:-2:-2])
# bytearray(b'H')

print(v[2:6])
print(v[-6:-2])
print(v[2:6:])
print(v[-6:-2:])
# bytearray(b'CDEF')

print(v[2:6:2])
print(v[-6:-2:2])
# bytearray(b'CE')

print(v[2:6:-2])
print(v[-6:-2:-2])
# bytearray(b'')
Enter fullscreen mode Exit fullscreen mode

A bytearray can be changed by indexing or slicing as shown below:

*Memo:

  • An iterable must be assigned to a sliced variable.
  • A del statement can be used to remove one or more bytes from a list by indexing or slicing and can remove one or more variables themselves.
v = bytearray(b'abcdef')

v[0] = ord(b'X')
v[2:6] = [ord(b'Y'), ord(b'Z')]

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

print(bytes(v))
# b'XbYZ'
Enter fullscreen mode Exit fullscreen mode
v = bytearray(b'abcdef')

del v[0], v[3:5]

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

print(bytes(v))
# b'bcd'
Enter fullscreen mode Exit fullscreen mode
v = bytearray(b'abcdef')

del v

print(v)
# NameError: name 'v' is not defined
Enter fullscreen mode Exit fullscreen mode

The variables v1 and v2 refer to the same bytearray unless copied as shown below:

*Memo:

  • is keyword or is and not keyword can check if v1 and v2 refer to the same bytearray respectively.
  • bytearray.copy(), copy.copy(), bytearray() and slicing do shallow copy:
    • bytearray.copy() has no arguments.
  • copy.deepcopy() does deep copy.
  • copy.deepcopy() should be used because it's safe, doing copy deeply while bytearray().copy(), copy.copy(), bytearray() and slicing aren't safe, doing copy shallowly.
import copy

v1 = bytearray(b'abcde')

v2 = v1 # v2 refers to the same bytearray as v1.

v2[2] = ord('X') # Changes the same bytearray as v1.
v2[2] = 88      
                        # ↓
print(v1) # bytearray(b'abXde')
print(v2) # bytearray(b'abXde') 
                        # ↑
print(v1 is v2, v1 is not v2)
# True False

v2 = v1.copy() # v2 refers to the different bytearray from v1.
v2 = copy.copy(v1)
v2 = copy.deepcopy(v1)
v2 = bytearray(v1)
v2 = v1[:]

v2[2] = ord('Y') # Changes the different bytearray from v1.
v2[2] = 89
                        # ↓
print(v1) # bytearray(b'abXde')
print(v2) # bytearray(b'abYde')
                        # ↑
print(v1 is v2, v1 is not v2)
# False True
Enter fullscreen mode Exit fullscreen mode

Top comments (0)