code1:
list=[1,2,3,4,5]
for i in range(len(list)):
for j in range(len(list)):
print(i+j)
code2:
list=[1,2,3,4,5]
l=len(list)
for i in range(l):
for j in range(l):
print(i+j)
Dose code2 faster than code1?
There will be no noticeable difference. len(somelist)
is a very fast O(1) operation. Lists have their length stored internally, so there's very little work to be done when you ask a list for its length.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With