I want to maintain a list of strings and keep checking for different variables if they are in the list.
What I’m doing is :
li=[one,two,three]
for x in different_x_values:
if(x in li):
print 'yes'
Is there a faster way than using x in li that checks if a some string is in some other collection of strings?
I am sure you already got your answer, this is for the sake of completeness. Out of curiosity I ran the tests for the methods mentioned I got following results.
Size=size of main list
setopttime: the one that uses s1&s2
setintersectiontime: the one using intersection operation
bruteforcetime:burte force time
Size=1000
setoptime:0:00:00.001000
setintersectiontime:0:00:00.001000
bruteforcetime:0:00:00.005000
Size=10000
setoptime:0:00:00.001000
setintersectiontime:0:00:00.010000
bruteforcetime:0:00:00.367000
Size=100000
setoptime:0:00:00.001000
setintersectiontime:0:00:00.115000
bruteforcetime:0:00:35.444000
Method 1 (setopttime) wins hands down. Here is the code if you want to check.