Hello!
I have a bunch of lists of strings and I need to know if an string is in any of them so I have to look for the string in the first list, if not found, in the second, if not found, in the third... and so on.
My question is: What is faster?
if (string in stringList1):
return True
else:
if (string in stringList2):
return True
# ... #
and so on or using the index() function inside a try / except block?
try:
return stringList1.index(string) >= 0
except:
try:
return stringList2.index(string) >= 0
except:
# ... #
I know the "in" is linear and that usually the python recommendations are "better say sorry than ask for permission" (meaning the second approach would be better) but I'd like to know the opinion of someone more qualified :)
Thank you!