range(len(product))
with enumerate(product)
883 product = products[0]
884 if product is None:
885 return
886 for i in range(len(product)): 887 self.__user_attributes[product[i].tag] = product[i].text
888 self.logger.debug("Parsed product info: {}".format(
889 self.__user_attributes))
range(len(self.alphabet))
with enumerate(self.alphabet)
110
111 def create_lookup_table(self):
112 self.lookup = bytearray(256)
113 for i in range(len(self.alphabet)):114 self.lookup[self.alphabet[i]] = i & 0xff
115
116 class CharacterSets:
range(len(indices))
with enumerate(indices)
63
64 def translate(self, indices: bytes, dictionary: bytes):
65 translation = bytearray(len(indices))
66 for i in range(len(indices)): 67 translation[i] = dictionary[int.from_bytes(indices[i].encode(),
68 "big")]
69 return translation
Using range(len(...))
is not pythonic. Python does not have not index-based loops. Instead, it uses collection iterators.
Python has a built-in method enumerate
which adds a counter to an iterable.
Using this, you can access the counter and the value from the iterable at the same time.
It is therefore recommended to replace range(len(...))
with enumerate(...)
.
for index in range(len(mylist)):
...
for index, element in enumerate(mylist):
...