PY3.C0207

Use maxsplit arg

Emitted when accessing only the first or last element of str.split(). The first and last element can be accessed by using str.split(sep, maxsplit=1)[0] or str.rsplit(sep, maxsplit=1)[-1] instead.

Noncompliant Code:

Copy
url = "www.example.com"
suffix = url.split(".")[-1]

Compliant Code:

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

Additional details

Be aware that the performance improvement from not splitting the string so many times will only be realized in cases presenting more instances of the splitting character than the minimal example here.

The content on this page is adapted from the Pylint User Guide, Copyright ©2003-2022, Logilab, PyCQA and contributors. All rights reserved. https://pylint.pycqa.org/en/latest/index.html#, and is used under the Python Software Foundation License Version 2. Examples, recipes, and other code in the Pylint documentation are additionally licensed under the Zero Clause BSD License.