len(seq) - 1
to get last element of an iterable PTC-W0044allmethod[-1]
instead of allmethod[len(allmethod) - 1]
to get the last element 94
95 allmethod += line
96
97 if allmethod[len(allmethod) - 1] == ")": 98 methodcomplete = 1
99
100 # clear the line
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]