How to compare a case insensitive string to 2 list in python

I need to compare a string against 2 list. I have the following list:

labels = ['admin', 'Admin', 'staff', 'techAdmin', 'techadmin']
accessCodeList = ['0x000', '0xAaA']

String Inputs:

mystring1 = "admin 0x000"
mystring2 = "demo 0x000"
mystring3 = "techAdmin 0x00"
mystring4 = "tech 0xAaA"
mystring5 = "techAdmin 0xAaA"
mystring6 = "Staff 0x000"   #-- starts with Uppercase


mystring1 = "Found"
mystring2 = "Not Found"
mystring3 = "Not Found"
mystring4 = "Not Found"
mystring5 = "Found"
mystring6 = "Found"  #-- case insensitive search result

>Solution :

Try converting labels and access_codes into a set for O(1) lookup time:

>>> labels = ['admin', 'Admin', 'staff', 'techAdmin', 'techadmin']
>>> access_codes = ['0x000', '0xAaA']
>>> inputs = ['admin 0x000', 'demo 0x000', 'techAdmin 0x00', 'tech 0xAaA', 'techAdmin 0xAaA', 'Staff 0x000']
>>> labels = {l.lower() for l in labels}
>>> access_codes = set(access_codes)
>>> for s in inputs:
...     label, code = s.split()
...     if label.lower() in labels and code in access_codes:
...         print(f'{s}, Found')
...     else:
...         print(f'{s}, Not Found')
admin 0x000, Found
demo 0x000, Not Found
techAdmin 0x00, Not Found  # 0x00 != 0x000 I think this is a typo in your example?
tech 0xAaA, Not Found
techAdmin 0xAaA, Found
Staff 0x000, Found

Leave a Reply