Remove Unused Enumerate¶
Sourcery refactoring id: remove-unused-enumerate
¶
Description¶
Remove unnecessary calls to
enumerate
when
the index variable is not used.
Before¶
for index, hat in enumerate(hats):
print("I like this hat: ", hat)
for i, (key, value) in enumerate(my_dictionary.items()):
do_something(key)
do_something_else(value)
beautiful_hats = [hat for hat_id, hat in enumerate(hats) if is_beautiful(hat)]
After¶
for hat in hats:
print("I like this hat: ", hat)
for key, value in my_dictionary.items():
do_something(key)
do_something_else(value)
beautiful_hats = [hat for hat in hats if is_beautiful(hat)]
Explanation¶
Enumerating iterables with
enumerate
is a
good practice for having access to both the values and their respective indices.
However, when the indices are not necessary, it is cleaner to simply iterate
over the original iterable and remove the call to enumerate
.