Skip to content

Remove dictionary keys

Sourcery refactoring id: remove-dict-keys

Description:

Removes unnecessary call to keys() when iterating over a dictionary

Before:

for currency in currencies.keys():
    process(currency)

After:

for currency in currencies:
    process(currency)

Explanation:

Sometimes when iterating over a dictionary you only need to use the dictionary keys.

In this case the call to keys() is unnecessary, since the default behaviour when iterating over a dictionary is to iterate over the keys.

The code is now slightly cleaner and easier to read, and avoiding a function call will yield a (small) performance improvement.

Back to top