bytes.rsplit()

Without maxsplit it is identical to split. With maxsplit it splits from the right, which is the whole reason it exists — take the last N fields and leave the rest joined.

Bytes methodPython 3.0+Live demo
Common call
*rest, last = data.rsplit(b'/', 1)
Returns
list of bytes, in original order
Replaces
rfind-and-slice for taking the last few fields
Watch out
the result is in LEFT-to-right order even though the split ran from the right
bytes.rsplit(sepsep — Separator. Omitted or None splits on runs of ASCII whitespace.type: bytes | None · default: None=None, maxsplitmaxsplit — Maximum number of splits, counted from the RIGHT. Negative means unlimited, at which point rsplit equals split.type: int · default: -1=-1)
→ list

Demo

Live evaluation
Try:
Inputs
sstrdata (encoded as utf-8)
sepstrseparator
maxsplitintmax splits from the right
Output
bytes('a,b,c', 'utf-8').rsplit(bytes(',', 'utf-8'), 1)
[b'a,b', b'c']

With maxsplit 1, a,b,c becomes [a,b, c] — one split, taken from the right, so the remainder stays joined on the LEFT. Compare split(b",", 1), which would give [a, b,c]. The list is still in left-to-right order; only the choice of which separators to honour changed. With maxsplit -1 the two methods are identical.

Parameters

NameTypeRequiredDescription
sepbytes | Noneno (None)Separator. Omitted or None splits on runs of ASCII whitespace.
maxsplitintno (-1)Maximum number of splits, counted from the RIGHT. Negative means unlimited, at which point rsplit equals split.

Return value

list — A list of bytes objects, in original order. With maxsplit, the splits are counted from the right, so the LEFTMOST element keeps whatever was not split.

Common patterns

Take the last path component
One split from the right leaves the directory joined.
directory, leaf = path.rsplit(b'/', 1)
Separate a trailing suffix or counter
Names like report-2024-final: keep the base, take the last piece.
base, tag = name.rsplit(b'-', 1)
Unpack the last N fields
Star-unpacking keeps the rest as a list.
*rest, penultimate, last = line.rsplit(b',', 2)

Examples

1. Last field
b'a,b,c'.rsplit(b',', 1)
Returns
[b'a,b', b'c']
2. split differs
b'a,b,c'.split(b',', 1)
Returns
[b'a', b'b,c']
3. Last two
b'a,b,c,d'.rsplit(b',', 2)
Returns
[b'a,b', b'c', b'd']
4. Unlimited
b'a,b,c'.rsplit(b',')
Returns
[b'a', b'b', b'c']
5. Path leaf
b'x/y/z'.rsplit(b'/', 1)
Returns
[b'x/y', b'z']
6. Absent
b'abc'.rsplit(b',', 1)
Returns
[b'abc']

Pitfalls

1. The output is in normal order
rsplit does not reverse the list. Only which separators get honoured changes, so the last field is still the last element.
Not reversed
b'a,b,c'.rsplit(b',', 1)
[b'a,b', b'c'] # c is still last
Index from the end
b'a,b,c'.rsplit(b',', 1)[-1]
b'c'
2. Without maxsplit it is just split
Reaching for rsplit with no limit gains nothing. It is only meaningful when maxsplit is set.
No difference
b'a,b,c'.rsplit(b',') == b'a,b,c'.split(b',')
True
Set a limit
b'a,b,c'.rsplit(b',', 1)
[b'a,b', b'c']
3. An absent separator gives one element
Unpacking into two names then fails with a confusing message. rpartition always gives three parts and avoids this.
Unpack fails
d, leaf = b'noslash'.rsplit(b'/', 1)
ValueError: not enough values to unpack (expected 2, got 1)
Use rpartition
d, sep, leaf = b'noslash'.rpartition(b'/')
always three

When to use

Use it
  • Taking the last N fields while keeping the rest joined
  • Path leaves, trailing tags and counters
  • Any rfind-and-slice that wants a list
Reach for something else
  • No maxsplit → plain split reads more naturally
  • Exactly one split, with safe unpacking → rpartition
  • The data is text → decode first

Notes

Complexity
O(n) — one scan from the right, plus an allocation per part
Return
A new list of new bytes objects, in original order
CPython impl
Objects/bytesobject.c :: bytes_rsplit
Memory
Allocates the list and every part
Thread-safe
Yes — bytes are immutable

FAQ

Only when maxsplit limits the number of splits. Then split honours the leftmost separators and rsplit the rightmost, so the joined remainder ends up on opposite sides. With no limit every separator is used and the results are identical.

b'a,b,c'.split(b',', 1)    # [b'a', b'b,c']
b'a,b,c'.rsplit(b',', 1)   # [b'a,b', b'c']

History

3.0
bytes.rsplit arrived with the bytes type in the text/binary split.