For that, we should know what makes python slow.
a = 1 # new Pyobject named a
a = ‘somestring’ # new Pyobject created
Reference count of old Pyobject is decreased to 0 so it can be collected by the garbage collector(slow part).
Using as low number of variable as possible.
x = result
return x
Fix could be returning x.
for x in range(10):
In every loop we redeclare x and old value is need to be garbage collected.
Use builtin methods for processes that are possible with builtin methods because they are implemented in C.
Or use a library like pandas, polars, and numpy etc.They use c|cpp under the hood.
Polars uses rust.
Using sets to remove copies from an array.
For more information