Skip to content

missing-maxsplit-arg (PLC0207)

Derived from the Pylint linter.

This rule is unstable and in preview. The --preview flag is required for use.

What it does

Checks for access to the first or last element of str.split() or str.rsplit() without maxsplit=1

Why is this bad?

Calling str.split() or str.rsplit() without passing maxsplit=1 splits on every delimiter in the string. When accessing only the first or last element of the result, it would be more efficient to only split once.

Example

url = "www.example.com"
prefix = url.split(".")[0]

Use instead:

url = "www.example.com"
prefix = url.split(".", maxsplit=1)[0]

To access the last element, use str.rsplit() instead of str.split():

url = "www.example.com"
suffix = url.rsplit(".", maxsplit=1)[-1]