Remove Unused Enumerate
Documentation Index
Fetch the complete documentation index at: https://docs.sourcery.ai/llms.txt
Use this file to discover all available pages before exploring further.
Sourcery refactoring id: remove-unused-enumerate
Section titled “Sourcery refactoring id: remove-unused-enumerate”Description
Section titled “Description”Remove unnecessary calls to
enumerate when
the index variable is not used.
Before
Section titled “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)]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
Section titled “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.