all Python 3Byte sequencesfunction
bytearray.join
bytearray — split and join
Join a sequence of bytes with this separator.
Syntax
ba.join(...)
Every element must be a bytes-like object (bytes, bytearray, memoryview): a plain str in the list raises TypeError, since there is no implicit encoding — call .encode() yourself. join walks the pieces once. Building a result with += is quadratic only for immutable bytes; a bytearray appends in place at amortised linear cost, so accumulating in one is fine, while a ready list of pieces is simplest to glue with a single join.
Examples
print(bytearray(b'-').join([b'a', b'b'])) # → bytearray(b'a-b')
print(bytearray(b'').join([b'ab', b'cd'])) # → bytearray(b'abcd')
print(bytearray(b', ').join([bytearray(b'x'), bytearray(b'y'), bytearray(b'z')])) # → bytearray(b'x, y, z')
print(bytearray(b'-').join([b'solo'])) # → bytearray(b'solo')
print(bytearray(b'-').join([])) # → bytearray(b'')
print(bytearray(b'-').join(['a', 'b'])) # → TypeError
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy