@@ -4,14 +4,18 @@ <h2>Why is this an issue?</h2>
44< code > key in dict.keys()</ code > , you are being unnecessarily verbose.</ p >
55< p > The expression < code > key in dict</ code > is the idiomatic Python way to check for key membership. It is:</ p >
66< ul >
7- < li > < em > more readable</ em > : the intent is clearer and the code is shorter</ li >
8- < li > < em > slightly more efficient</ em > : it avoids creating an intermediate dictionary view object</ li >
9- < li > < em > more Pythonic</ em > : it follows Python' s principle of having one obvious way to do it</ li >
7+ < li > < strong > more readable</ strong > : the intent is clearer and the code is shorter</ li >
8+ < li > < strong > slightly more efficient</ strong > : it avoids creating an intermediate dictionary view object</ li >
9+ < li > < strong > more Pythonic</ strong > : it follows Python’ s principle of having < em > one obvious way to do it</ em > </ li >
1010</ ul >
1111< p > The < code > .keys()</ code > method exists primarily for cases where you need to perform set operations on dictionary keys or when you need an explicit
1212view object. For simple membership testing, it is redundant because dictionaries are designed to support the < code > in</ code > operator directly.</ p >
13+ < h3 > What is the potential impact?</ h3 >
14+ < p > Unnecessarily verbose code patterns accumulate over a codebase and reduce readability. Following Python idioms makes code easier to review and
15+ maintain, and helps new contributors understand the codebase faster.</ p >
1316< h2 > How to fix it</ h2 >
14- < p > Remove the < code > .keys()</ code > call and use direct membership testing on the dictionary.</ p >
17+ < p > Remove the < code > .keys()</ code > call and use direct membership testing on the dictionary. This is the standard Python idiom for checking if a key
18+ exists in a dictionary.</ p >
1519< h3 > Code examples</ h3 >
1620< h4 > Noncompliant code example</ h4 >
1721< pre data-diff-id ="1 " data-diff-type ="noncompliant ">
@@ -33,3 +37,4 @@ <h3>Documentation</h3>
3337 < li > Python Documentation - < a href ="https://docs.python.org/3/library/stdtypes.html#mapping-types-dict "> Mapping Types — dict</ a > </ li >
3438 < li > Python Documentation - < a href ="https://docs.python.org/3/library/stdtypes.html#dictionary-view-objects "> Dictionary view objects</ a > </ li >
3539</ ul >
40+
0 commit comments