Skip to content

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(), start=100):
    do_something(key)
    do_something_else(value)

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)

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.

Back to top