Let's say that we have to use the length of a list in certain calculations in a loop. Which would be faster, using len(list_)
in each calculation or storing the length length = len(list_)
and then using length
? For example:
for x in range(n):
print(len(list_) + 1)
Versus
length = len(list_)
for x in range(n):
print(length + 1)
Assume a generic situation (n
can be any value).