Search code examples
pythonpython-3.xsumbyte

Why sum() doesn't work on array of bytes elements in python?


This code works perfectly:

b'\x4a' + b'\x20'
b'J '

But this doesn't:

sum([b'\x4a', b'\x20'])
TypeError: unsupported operand type(s) for +: 'int' and 'bytes'

Why? How to concatenate many bytes elements?


Solution

  • You can use join instead:

    b''.join([b'\x4a', b'\x20'])
    

    Output:

    b'J '