Unnecessary use of
list()on an already iterable type.
Incorrect iterator method for
dict: Python dictionaries store keys and values in two separate tables. They can be iterated separately. Using
.items()and discarding either the key or the value with
_is inefficient when
.values()can be used instead.
The loop is examined to determine statements or expressions whose result is constant on each iteration of a loop because they are based on named variables that are not changed during the iteration.
Global name usage in a loop: loading global variables is slower than loading local variables. The difference is marginal, but when passed in a loop, there can be a noticeable speed improvement.
Up until Python 3.10,
exceptblocks are very computationally intensive compared to
Avoid using them in a loop as they can cause significant overhead. Refactor your code so that no iteration-specific details are required and put the entire loop in the
Slicing byte objects in loops is inefficient because it creates a copy of the data. Use
Direct import of the name
%sis more efficient in a loop. In Python, you can import a module and then access submodules as attributes. You can also access functions as attributes of that module. This keeps the import statements to a minimum. However, if you use this method in a loop, it is inefficient because each loop pass loads the global, then the attribute, then the method.
Use a tuple instead of a list for an immutable sequence: both the construction and indexing of a tuple is faster than that of a list.
Use list comprehensions with or without an
ifstatement instead of a
list.copy()instead of a
Uses a dictionary comprehension instead of a simple