range(len(l))
with enumerate(l)
71
72def build_test_data(destdir):
73 l = get_test_data_list()
74 for i in range(len(l)): 75 # packer = msgpack.Packer()
76 serialized = msgpack.dumps(l[i])
77 with open(os.path.join(destdir, str(i) + '.msgpack.golden'), 'wb') as f:
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):
...