2

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?

1
  • 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? Commented May 28, 2018 at 10:07

7 Answers 7

5

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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 

Comments

3

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

1 Comment

Casefold does not work if the dictionary keys are Capitalized.
0

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 

Comments

0

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

Comments

0

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) 

4 Comments

@Andrea Foderaro Please accept the answer. It does work for you.
@Marichyasana He wants case insensitive. (as mentioned in the question)
What means "case insensitive?" The keys can contain upper case in which the match fails.
Sorry @Leo this isn't case insensitive
0

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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.