# Access python dictionary with case insensitive string

I am struggling to see if there exist a way in order to access a python dictionary with strings as keys with the correct string but case insensitive

For example with this code:

``````dictionary = {'one': 1, 'two': 2, 'three', 3}
list = ['One', 'second', 'THree']

for item in list:
if item in dictionary:
print(item)
``````

I am not able to find a way such that the output is: `One THree', ie. to compare the keys of the dictionary and the string that I have case insenstively

Is there a way to do that?

• Not unless you inherit from `defaultdict` and modify the getter. But think about this: if you have the keys `one` and `ONE`, then what value would you like to get with `One`? – Mike Scotty May 28 '18 at 10:07

You need to handle case-insenstivity on your end by either converting your `dictionary.keys()` to lower case or `list` elements to lower case or both as:

``````for item in list:
if item.lower() in map(str.lower, dictionary.keys()):
print(item)
``````

You can either use `lower()` or `upper()`, but you have to be consistent in both the cases.

Create a list of lowercase dictionary keys, then compare the lowercase variants of each item in your list with this list.

``````dictionary = {'one': 1, 'two': 2, 'three': 3}
list = ['One', 'second', 'THree']

for item in list:
if item.lower() in [key.lower() for key in dictionary.keys()]:
print(item)
``````

Thus:

``````One
THree
``````

use string method `lower`:

``````dictionary = {'one': 1, 'two': 2, 'three': 3}
list = ['One', 'second', 'THree']

for item in list:
if item.lower() in dictionary:
print(item)
``````

Output:

``````One
THree
``````

Use `casefold()` for case insensitive search:

``````dictionary = {'one': 1, 'two': 2, 'three': 3}
list = ['One', 'second', 'THree']

for item in list:
if item.casefold() in dictionary:
print(item)
``````

A list-comprehension way:

``````print( '\n'.join([item for item in list if item.casefold() in dictionary]))
``````
• Casefold does not work if the dictionary keys are Capitalized. – Nirmal Apr 27 '20 at 0:17

Try using the python `lambda` keyword:

``````l = ['One', 'second', 'THree']
lc = lambda x: [i for i in x if i.lower() in dictionary]
print(lc(l))
``````

Output:

``````['One', 'THree']
``````

Note: i changed the variable name `list` to `l` because list is a python keyword so if you want to call the list keyword it won't call the list keyword instead it will call the list

Try this:

``````dictionary = {'one': 1, 'two': 2, 'three': 3}
list = ['One', 'second', 'THree']

for item in list:
if item.lower() in [d.lower() for d in dictionary.keys()]:
print(item)
``````
• @Andrea Foderaro Please accept the answer. It does work for you. – Hayat May 28 '18 at 10:10
• @Marichyasana He wants case insensitive. (as mentioned in the question) – Hayat May 28 '18 at 10:13
• What means "case insensitive?" The keys can contain upper case in which the match fails. – Marichyasana May 28 '18 at 10:16
• Sorry @Leo this isn't case insensitive – Andrea Foderaro May 28 '18 at 17:54

The right way of doing it is to use casefold function. This function available from Python 3.3.

Return a casefolded copy of the string. Casefolded strings may be used for caseless matching.

Casefolding is similar to lowercasing but more aggressive because it is intended to remove all case distinctions in a string. For example, the German lowercase letter 'ß' is equivalent to "ss". Since it is already lowercase, lower() would do nothing to 'ß'; casefold() converts it to "ss".

The casefolding algorithm is described in section 3.13 of the Unicode Standard.

``````dictionary = {"one": 1, "two": 2, "three": 3}
list = ["One", "second", "THree"]

print(*(item for item in list if item.casefold() in dictionary), sep="\n")
``````

Output:

``````One
THree
``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.8389127,"math_prob":0.697968,"size":661,"snap":"2021-04-2021-17","text_gpt3_token_len":178,"char_repetition_ratio":0.11567732,"word_repetition_ratio":0.0,"special_character_ratio":0.27231467,"punctuation_ratio":0.13286713,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95889336,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-01-26T22:09:49Z\",\"WARC-Record-ID\":\"<urn:uuid:b7ac7e72-9dd9-4d75-8780-939871ec01a9>\",\"Content-Length\":\"207340\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:52b8b132-172f-4689-b86b-8adbd2dea220>\",\"WARC-Concurrent-To\":\"<urn:uuid:12ddf6ff-cc41-48fc-98b4-164e31f747eb>\",\"WARC-IP-Address\":\"151.101.129.69\",\"WARC-Target-URI\":\"https://stackoverflow.com/questions/50563844/access-python-dictionary-with-case-insensitive-string/50564391\",\"WARC-Payload-Digest\":\"sha1:RKE4VNMBVABHUAP2IPLSFTXLGWG25DRD\",\"WARC-Block-Digest\":\"sha1:BHBUVO5MRX5ZWMF2PWTYKSEYCT27V3YJ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-04/CC-MAIN-2021-04_segments_1610704803737.78_warc_CC-MAIN-20210126202017-20210126232017-00538.warc.gz\"}"}