There is a nice class Enum from enum, but it only works for strings. I’m currently using:
for index in range(len(objects)):
# do something with index and objects[index]
I guess it’s not the optimal solution due to the premature use of len. How is it possible to do it more efficiently?
Here is the pythonic way to write this loop:
enumerateworks on any sequence regardless of the types of its elements. It is a builtin function.Edit:
After running some
timeittests using Python 2.5, I foundenumerateto be slightly slower: