Andrea Foderaro
Andrea Foderaro

Reputation: 45

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?

Upvotes: 2

Views: 13018

Answers (7)

Vlad Bezden
Vlad Bezden

Reputation: 89527

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

Upvotes: 0

Hayat
Hayat

Reputation: 1639

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)

Upvotes: 0

U13-Forward
U13-Forward

Reputation: 71570

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

Upvotes: 0

Austin
Austin

Reputation: 26039

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]))

Upvotes: 3

Resin Drake
Resin Drake

Reputation: 546

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

Upvotes: 3

ZdaR
ZdaR

Reputation: 22954

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.

Upvotes: 5

akshat
akshat

Reputation: 1217

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

Upvotes: 0

Related Questions