Python

Python

Made by DeepSource

Use of len(seq) - 1 to get last element of an iterable PTC-W0044

Performance
Major
Autofix

There’s no need to calculate length of an iterable in order to fetch the last element of the iterable. You can provide a negative index -1 to it directly in orger to get the last element. In this way, you don't have to iterate over the sequence using len to get the last index when your purpose is only to get the last element.

Not performant code:

last_element = some_sequence[len(some_sequence) - 1]

Performant Code:

last_element = some_sequence[-1]