Home > OS >  I have a variable value in lower case and the same value is in one of the dictionary keys how do I f
I have a variable value in lower case and the same value is in one of the dictionary keys how do I f

Time:11-18

i have document_title variable value with lowercase letters and same value is in the dic keys with upercase letter


TITLE_MAP = {
    'AUS Marketing Consent': "DOCUMENT_TYPE_MARKETING_CONSENT",
    'Consent & History': "DOCUMENT_TYPE_CONSENT",
}

document_title = 'aus marketing consent'



if i do this won't work with me

if document_title in TITLE_MAP.keys():
   return True 

I want to fulfill the condition even with the difference

CodePudding user response:

You can use the casefold method to do string comparison. Since you want to apply it to all the keys, you can use a list comprehension.

if document_title.casefold() in [x.casefold() for x in TITLE_MAP.keys()]:
    print(True)

Hope this helps.

CodePudding user response:

if document_title.upper() in TITLE_MAP.key():
   return True

CodePudding user response:

Maybe it's overkill but you can try this solution :

if document_title.lower() in {k.lower() for k in TITLE_MAP.keys()}:
    print(True)

It lowers every keys from your dictionnary

CodePudding user response:

The two strings must be in the same case. You have to convert all keys to lowercase. Try the code below

TITLE_MAP = {
    'AUS Marketing Consent': "DOCUMENT_TYPE_MARKETING_CONSENT",
    'Consent & History': "DOCUMENT_TYPE_CONSENT",
}

TITLE_MAP = {k.lower(): v for k, v in TITLE_MAP.items()}

document_title = 'aus marketing consent'

if document_title.lower() in TITLE_MAP:
    print(True)
  • Related