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.
The method doesn't use its bound instance. Decorate this method with @staticmethod
decorator, so that Python does not have to instantiate a bound method for every instance of this class thereby saving memory and computation. Read more about staticmethods here.
The built-in function being used does not require comprehension and can work directly with a generator expression. Using a generator expession within these functions is faster than using a comprehension.
Using the literal syntax can give minor performance bumps compared to using function calls to create dict
, list
and tuple
.
Consider using str.join(sequence)
instead of joining the elements of a sequence using for
loop iteration.
str.join(sequence)
is faster, uses less memory and increases readability compared to a for
loop iteration.