Performance anti-patterns
These anti-patterns lead to inefficiencies that can degrade performance, especially noticeable in large-scale applications or data-intensive tasks. We will focus on the following such anti-patterns:
- Not using
.join()to concatenate strings in a loop - Using global variables for caching
Let’s start.
Not using .join() to concatenate strings in a loop
Concatenating strings with + or += in a loop creates a new string object each time, which is inefficient. The best solution is to use the .join() method on strings, which is designed for efficiency when concatenating strings from a sequence or iterable.
Let’s create a concatenate() function where we use += for concatenating items from a list of strings, as follows:
def concatenate(string_list):     result = ""     for item in string_list:         result += item    &...