Search code examples
arrayspython-3.xconcatenation

Python: join two bytearray objects


I would like to concatenate a bytearray to another bytearray. I thought this might work:

byt1 = bytearray(10)
byt2 = bytearray(10)
byt1.join(byt2)
print(repr(byt1))

byt1.join(byt2)

TypeError: sequence item 0: expected a bytes-like object, int found

What is the most efficient way to achieve this?


Solution

  • Create a new combined bytearray from two:

    byt_combined = byt1 + byt2
    

    Extend one bytearray with another. This changes byt1:

    byt1.extend(byt2)