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.
Demo
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
| Name | Type | Required | Description |
|---|---|---|---|
| sep | bytes | None | no (None) | Separator. Omitted or None splits on runs of ASCII whitespace. |
| maxsplit | int | no (-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
directory, leaf = path.rsplit(b'/', 1)
base, tag = name.rsplit(b'-', 1)
*rest, penultimate, last = line.rsplit(b',', 2)
Examples
Pitfalls
b'a,b,c'.rsplit(b',', 1)
b'a,b,c'.rsplit(b',', 1)[-1]
b'a,b,c'.rsplit(b',') == b'a,b,c'.split(b',')
b'a,b,c'.rsplit(b',', 1)
d, leaf = b'noslash'.rsplit(b'/', 1)
d, sep, leaf = b'noslash'.rpartition(b'/')
When to use
- Taking the last N fields while keeping the rest joined
- Path leaves, trailing tags and counters
- Any rfind-and-slice that wants a list
- No maxsplit → plain split reads more naturally
- Exactly one split, with safe unpacking → rpartition
- The data is text → decode first
Notes
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']