do not modify a dictionary while iterating on it
このページは日本語には対応しておりません。随時翻訳に取り組んでいます。翻訳に関してご質問やご意見ございましたら、お気軽にご連絡ください。
ID: python-best-practices/collection-while-iterating
Language: Python
Severity: Error
Category: Error Prone
Description
Never update a dictionary while iterating on it. If you wish to update the dictionary, create a new dictionary from the existing values.
Non-Compliant Code Examples
i = 0
for element in my_list:
my_list["stuff"] = i # modifying a dictionary while iterating
i += 1
Compliant Code Examples
i = 0
new_list = {}
for element in my_list:
new_list["stuff"] = i # putting value to a new dictionary
i += 1
Seamless integrations. Try Datadog Code Analysis