bytearray.reverse()

Reverses BYTES, not characters. On UTF-8 encoded text that produces an invalid sequence, which is the single most important thing to know about it.

Bytearray methodPython 3.0+Live demo
Common call
buf.reverse()
Returns
None — the buffer is reversed in place
Replaces
buf[:] = buf[::-1], which allocates a copy first
Watch out
reversing encoded text corrupts every multi-byte character
bytearray.reverse()
→ None

Demo

Live evaluation
Try:
Inputs
sstrbuffer (as text)
Output
[b := bytearray(bytes('abc', 'utf-8')), b.reverse(), b][2]
bytearray(b'cba')

For ASCII the result is what you would expect — abc becomes cba. The last case is the warning. "héllo" is encoded as six UTF-8 bytes, and reversing them puts the two bytes of the accented character in the wrong order, producing a sequence that is no longer valid UTF-8 and will raise on decode. Byte reversal and text reversal are different operations.

Common patterns

Flip byte order in place
Converting between endiannesses for a fixed-width field.
field = bytearray(raw)
field.reverse()
Reverse without allocating
The in-place form avoids the copy that slicing makes.
buf.reverse()   # not buf = buf[::-1]
Build backwards, then flip
Appending and reversing once beats repeatedly inserting at the front.
for v in values:
    buf.append(v)
buf.reverse()

Examples

1. ASCII
b = bytearray(b'abc') b.reverse() b
Returns
bytearray(b'cba')
2. Returns None
bytearray(b'abc').reverse()
Returns
None
3. Palindrome
b = bytearray(b'aba') b.reverse() b
Returns
bytearray(b'aba')
4. Empty
b = bytearray() b.reverse() b
Returns
bytearray(b'')
5. Breaks UTF-8
b = bytearray('é'.encode()) b.reverse() bytes(b).decode()
Returns
UnicodeDecodeError
6. Text reversal differs
'héllo'[::-1]
Returns
'olléh'

Pitfalls

1. It corrupts UTF-8 text
Multi-byte characters have an ordered lead byte and continuation bytes. Reversing puts them backwards, so the result is not valid UTF-8 and fails on decode — often far from where the reversal happened.
Invalid sequence
b = bytearray('é'.encode())
b.reverse()
bytes(b).decode()
UnicodeDecodeError
Reverse the text
'héllo'[::-1].encode()
valid, and correct
2. It returns None
The same trap as every in-place mutator. Assigning the result leaves you holding None instead of a buffer.
Buffer lost
buf = buf.reverse()
None
Just call it
buf.reverse()
buf is reversed
3. Not the same as slicing
buf[::-1] returns a NEW reversed bytearray and leaves the original alone; reverse mutates and returns nothing. Swapping one for the other changes both the result and who sees it.
Original untouched
b = bytearray(b"abc")
b[::-1]
b
bytearray(b'abc')
Mutate in place
b.reverse()
bytearray(b'cba')
4. Endianness needs a fixed width
Reversing a whole buffer only flips byte order correctly when it holds exactly one value. On a buffer of several fields it reverses the fields as well, scrambling the layout.
Whole buffer flipped
buf.reverse()   # buffer holds 3 fields
field order reversed too
Per field
import struct
struct.unpack('>3H', data)
explicit endianness

When to use

Use it
  • Flipping byte order within a single fixed-width value
  • Reversing a buffer without allocating a copy
  • Building a sequence backwards and correcting it once at the end
Reach for something else
  • Encoded text → reverse the str, then encode
  • Multi-field binary layouts → struct with explicit endianness
  • You need the original kept → slice with [::-1] instead

Notes

Complexity
O(n) — swaps bytes from both ends inward
Return
None; the bytearray is mutated in place and keeps its identity
CPython impl
Objects/bytearrayobject.c :: bytearray_reverse
Memory
No allocation — unlike slicing, which copies
Thread-safe
Not safe under concurrent mutation of the same buffer

FAQ

Because UTF-8 encodes non-ASCII characters as several ordered bytes. Reversing the byte sequence reverses those too, producing continuation bytes before their lead byte — which is not valid UTF-8. Reverse the string, then encode.

'héllo'[::-1].encode('utf-8')

History

3.0
bytearray introduced as the mutable counterpart to bytes.