(Python) I want to check if a variable A is equal to a string B or a string C, should i do
if A==B or A==C
or
if A in [B, C]
instead? both work, but im not sure which is faster or performs better, or if there is a common convention
Asked
Active
Viewed 17 times
0
pollatron
- 26
- 2
-
Hello, I think you can add both ```B``` and ```C``` to a list and just check if that variable, say ```A``` is in that list, that way if you want to compare ```A``` with other values later that are not ```B``` or ```C``` you can do it easily. I'd go with something like this ``` string_list = ["B", "C"] var = "B" if var in string_list: print("Variable is in the list") # if needed to print anything ``` – hunter Mar 23 '22 at 02:30