Skip to content

Latest commit

 

History

History
32 lines (22 loc) · 661 Bytes

File metadata and controls

32 lines (22 loc) · 661 Bytes

R1714 (consider-using-in)

❌ Problematic code:

array = list(range(10))
n = input()

for elem in array:
    if int(n) == elem:
        pass

✔️ Correct code:

array = list(range(10))
n = input()

if int(n) in array:
    pass

Rationale:

To check if a variable is equal to one of many values,combine the values into a tuple and check if the variable is contained in it instead of checking for equality against each of the values. This is faster and less verbose.

Related resources: