Python

Python

Made by DeepSource

Use items() to iterate over a dictionary PTC-W0011

Anti-pattern
Major

The preferred way to iterate over the key-value pairs of a dictionary is to declare two variables in a for loop, and then call dictionary.items() (or dictionary.iteritems() for Python2), where dictionary is the name of your variable representing a dictionary.'

Not preferred

details = {"first_name": "Alfred", "last_name":"Hitchcock"}
for key in details.keys():
    print(key, d[key])

Preferred

details = {"first_name": "Alfred", "last_name":"Hitchcock"}
for key, value in details.items():
    print(key, value)