111

I'd like my dictionary to be case insensitive.

I have this example code:

text = "practice changing the color"

words = {'color': 'colour',
        'practice': 'practise'}

def replace(words,text):

    keys = words.keys()

    for i in keys:
        text= text.replace(i ,words[i])
    return  text

text = replace(words,text)

print text

Output = practise changing the colour

I'd like another string, "practice changing the Color", (where Color starts with a capital) to also give the same output.

I believe there is a general way to convert to lowercase using mydictionary[key.lower()] but I'm not sure how to best integrate this into my existing code. (If this would be a reasonable, simple approach anyway).

2
  • 6
    @NickT This PEP has been rejected. python.org/dev/peps/pep-0455/#rejection Commented Apr 8, 2016 at 15:17
  • I am probably missing the point, but in a simplistic way: words = {'color': 'colour', 'Color': 'Colour', 'practice': 'practise', 'Practice', 'Practise'}. The most obvious problems that you are going to run into are changing partial words that do not need changing (“Technicolor”) or switching the British English noun form “practice” to the verb form “practise”. Need a proofreader, really.
    – typonaut
    Commented Nov 12, 2022 at 15:35

13 Answers 13

91

Just for the record. I found an awesome impementation on Requests:

https://github.com/kennethreitz/requests/blob/v1.2.3/requests/structures.py#L37

5
  • 30
    from requests.structures import CaseInsensitiveDict
    – JimB
    Commented May 8, 2016 at 20:55
  • 17
    That might work, but if what you need is just a Case Insensitive Dict, it's silly to add requests as a dependency just for that. Commented May 9, 2016 at 13:33
  • 18
    @santiagobasulto - it's "silly" until the (need-it-to-work/time-till-deadline) ratio goes to infinity
    – qneill
    Commented Aug 16, 2020 at 20:57
  • If you need just the CaseInsensitiveDict you are free to lift just the source-code of that class from the requests project and insert it into your project provided you retain the license properly etc.
    – Donal
    Commented Sep 28, 2022 at 19:51
  • Note: The structure remembers the case of the last key to be set, and iter(instance), keys(), items(), iterkeys(), and iteritems() will contain case-sensitive keys. However, querying and contains testing is case insensitive
    – Hritik
    Commented Jan 5, 2023 at 18:50
90

The currently accepted answer wouldn't work for lots of cases, so it cannot be used as a drop-in dict replacement. Some tricky points in getting a proper dict replacement:

  • overloading all of the methods that involve keys
  • properly handling non-string keys
  • properly handling the constructor of the class

The following should work much better:

class CaseInsensitiveDict(dict):
    @classmethod
    def _k(cls, key):
        return key.lower() if isinstance(key, basestring) else key

    def __init__(self, *args, **kwargs):
        super(CaseInsensitiveDict, self).__init__(*args, **kwargs)
        self._convert_keys()
    def __getitem__(self, key):
        return super(CaseInsensitiveDict, self).__getitem__(self.__class__._k(key))
    def __setitem__(self, key, value):
        super(CaseInsensitiveDict, self).__setitem__(self.__class__._k(key), value)
    def __delitem__(self, key):
        return super(CaseInsensitiveDict, self).__delitem__(self.__class__._k(key))
    def __contains__(self, key):
        return super(CaseInsensitiveDict, self).__contains__(self.__class__._k(key))
    def has_key(self, key):
        return super(CaseInsensitiveDict, self).has_key(self.__class__._k(key))
    def pop(self, key, *args, **kwargs):
        return super(CaseInsensitiveDict, self).pop(self.__class__._k(key), *args, **kwargs)
    def get(self, key, *args, **kwargs):
        return super(CaseInsensitiveDict, self).get(self.__class__._k(key), *args, **kwargs)
    def setdefault(self, key, *args, **kwargs):
        return super(CaseInsensitiveDict, self).setdefault(self.__class__._k(key), *args, **kwargs)
    def update(self, E={}, **F):
        super(CaseInsensitiveDict, self).update(self.__class__(E))
        super(CaseInsensitiveDict, self).update(self.__class__(**F))
    def _convert_keys(self):
        for k in list(self.keys()):
            v = super(CaseInsensitiveDict, self).pop(k)
            self.__setitem__(k, v)
16
  • 3
    This is great, but there is one minor problem. The super definition of update is update(self, E=None, **F), meaning E is optional. You've re-defined it to make E required. Add in the =None and this will be perfect. Commented Nov 16, 2015 at 14:44
  • 55
    Python is easy, they said. Python is fun, they said.
    – rr-
    Commented Nov 29, 2015 at 20:59
  • 12
    @rr-. To be totally fair, imagine doing this in say C. Commented Jul 5, 2016 at 14:38
  • 1
    @MadPhysicist Not sure, but it should be straightforward to add. Just modify _k() to also normalise as desired.
    – m000
    Commented Jul 5, 2016 at 18:48
  • 13
    In python 3 the abstract type basestring was removed. str can be used as a replacement.
    – Jan Schatz
    Commented Jan 19, 2018 at 9:47
50

If I understand you correctly and you want a way to key dictionaries in a non case-sensitive fashion, one way would be to subclass dict and overload the setter / getter:

class CaseInsensitiveDict(dict):
    def __setitem__(self, key, value):
        super(CaseInsensitiveDict, self).__setitem__(key.lower(), value)

    def __getitem__(self, key):
        return super(CaseInsensitiveDict, self).__getitem__(key.lower())
6
  • 1
    Isn't there a special builtin that is called for 'in' as well? Commented Jan 17, 2010 at 19:00
  • 30
    Here is a complete list of methods that may need overloading: setitem, getitem, contains, get, has_key, pop, setdefault, and update. init and fromkeys should also possibly be overloaded to make sure the dictionary is initialized properly. Maybe I'm wrong and somewhere Python promises that get, hash_key, pop, setdefault, update and init will be implemented in terms of getitem, setitem and contains if they've been overloaded, but I don't think so. Commented Jan 17, 2010 at 19:08
  • 5
    added __contains__, get, and has_key to the answer since I ended up coding them :) Commented Apr 8, 2011 at 23:29
  • 12
    This solution is very limited as it doesn't work for a lot of common uses of dict. Don't use it in your code - it will break all but the simplest uses. Apparently @MichaelMerchant attempted to add the missing stuff, but moderation dissaproved the changes (same thing happened to me). I added a new answer which should be usable as a drop-in dict replacement here.
    – m000
    Commented Oct 1, 2015 at 13:26
  • 3
    Better off subclassing UserDict than dict docs.python.org/3.5/library/collections.html#userdict-objects
    – rite2hhh
    Commented Aug 30, 2019 at 19:37
20

In my particular instance, I needed a case insensitive lookup, however, I did not want to modify the original case of the key. For example:

>>> d = {}
>>> d['MyConfig'] = 'value'
>>> d['myconfig'] = 'new_value'
>>> d
{'MyConfig': 'new_value'}

You can see that the dictionary still has the original key, however it is accessible case-insensitively. Here's a simple solution:

class CaseInsensitiveKey(object):
    def __init__(self, key):
        self.key = key
    def __hash__(self):
        return hash(self.key.lower())
    def __eq__(self, other):
        return self.key.lower() == other.key.lower()
    def __str__(self):
        return self.key

The __hash__ and __eq__ overrides are required for both getting and setting entries in the dictionary. This is creating keys that hash to the same position in the dictionary if they are case-insensitively equal.

Now either create a custom dictionary that initializes a CaseInsensitiveKey using the provided key:

class CaseInsensitiveDict(dict):
    def __setitem__(self, key, value):
        key = CaseInsensitiveKey(key)
        super(CaseInsensitiveDict, self).__setitem__(key, value)
    def __getitem__(self, key):
        key = CaseInsensitiveKey(key)
        return super(CaseInsensitiveDict, self).__getitem__(key)

or simply make sure to always pass an instance of CaseInsensitiveKey as the key when using the dictionary.

2
  • Nice, thanks! :) (Note that this class doesn't implement the case-insensitive "dict(iterable)" constructor so if you need it you have to add it)
    – Joril
    Commented Mar 27, 2018 at 9:15
  • 4
    You should use .casefold() instead of .lower() for comparisons, self.key.casefold() == other.key.casefold(), to allow "ß" and "ss" to equate as true, among others.
    – AJNeufeld
    Commented Sep 24, 2019 at 20:00
15

Would you consider using string.lower() on your inputs and using a fully lowercase dictionary? It's a bit of a hacky solution, but it works

5
  • It's a bit hacky, but I think it is along the lines of what Kim was after.
    – John Y
    Commented Jan 18, 2010 at 7:42
  • This is not hacky. In fact this is the less error-prone way than overriding dictionary class. Commented Oct 15, 2016 at 23:24
  • 5
    This is great unless you want to preserve the original case when setting the a key the first time. Commented Nov 26, 2016 at 16:01
  • 3
    use string.casefold() instead Commented Mar 3, 2021 at 22:34
  • 2
    @ErikAronesty link to docs: docs.python.org/library/stdtypes.html#str.casefold
    – user3064538
    Commented Jul 5, 2021 at 1:44
6

I've modified the simple yet good solution by pleasemorebacon (thanks!) making it slightly more compact, self-contained and with minor updates to allow construction from {'a':1, 'B':2} and support __contains__ protocol. Finally, since the CaseInsensitiveDict.Key is expected to be string (what else can be case-sensitive or not), it is a good idea to derive Key class from the str, then it is possible, for instance, to dump CaseInsensitiveDict with json.dumps out of the box.

# caseinsensitivedict.py
class CaseInsensitiveDict(dict):

    class Key(str):
        def __init__(self, key):
            str.__init__(key)
        def __hash__(self):
            return hash(self.lower())
        def __eq__(self, other):
            return self.lower() == other.lower()

    def __init__(self, data=None):
        super(CaseInsensitiveDict, self).__init__()
        if data is None:
            data = {}
        for key, val in data.items():
            self[key] = val
    def __contains__(self, key):
        key = self.Key(key)
        return super(CaseInsensitiveDict, self).__contains__(key)
    def __setitem__(self, key, value):
        key = self.Key(key)
        super(CaseInsensitiveDict, self).__setitem__(key, value)
    def __getitem__(self, key):
        key = self.Key(key)
        return super(CaseInsensitiveDict, self).__getitem__(key)

Here is a basic test script for those who like to check things in action:

# test_CaseInsensitiveDict.py
import json
import unittest
from caseinsensitivedict import *

class Key(unittest.TestCase):
    def setUp(self):
        self.Key = CaseInsensitiveDict.Key
        self.lower = self.Key('a')
        self.upper = self.Key('A')

    def test_eq(self):
        self.assertEqual(self.lower, self.upper)

    def test_hash(self):
        self.assertEqual(hash(self.lower), hash(self.upper))

    def test_str(self):
        self.assertEqual(str(self.lower), 'a')
        self.assertEqual(str(self.upper), 'A')

class Dict(unittest.TestCase):
    def setUp(self):
        self.Dict = CaseInsensitiveDict
        self.d1 = self.Dict()
        self.d2 = self.Dict()
        self.d1['a'] = 1
        self.d1['B'] = 2
        self.d2['A'] = 1
        self.d2['b'] = 2

    def test_contains(self):
        self.assertIn('B', self.d1)
        d = self.Dict({'a':1, 'B':2})
        self.assertIn('b', d)

    def test_init(self):
        d = self.Dict()
        self.assertFalse(d)
        d = self.Dict({'a':1, 'B':2})
        self.assertTrue(d)

    def test_items(self):
        self.assertDictEqual(self.d1, self.d2)
        self.assertEqual(
            [v for v in self.d1.items()],
            [v for v in self.d2.items()])

    def test_json_dumps(self):
        s = json.dumps(self.d1)
        self.assertIn('a', s)
        self.assertIn('B', s)

    def test_keys(self):
        self.assertEqual(self.d1.keys(), self.d2.keys())

    def test_values(self):
        self.assertEqual(
            [v for v in self.d1.values()],
            [v for v in self.d2.values()])
2
  • 2
    You should use .casefold() instead of .lower() for comparisons, self.casefold() == other.key.casefold() and hash(self.casefold()), to allow "ß" and "ss" to equate as true, among others.
    – AJNeufeld
    Commented Sep 24, 2019 at 20:11
  • A small addition for nested dictionaries: Commented Feb 1, 2022 at 8:48
4

You can do a dict key case insensitive search with a one liner:

>>> input_dict = {'aBc':1, 'xyZ':2}
>>> search_string = 'ABC'
>>> next((value for key, value in input_dict.items() if key.lower()==search_string.lower()), None)
1
>>> search_string = 'EFG'
>>> next((value for key, value in input_dict.items() if key.lower()==search_string.lower()), None)
>>>

You can place that into a function:


def get_case_insensitive_key_value(input_dict, key):
    return next((value for dict_key, value in input_dict.items() if dict_key.lower() == key.lower()), None)


Note that only the first match is returned.

0
3

While a case insensitive dictionary is a solution, and there are answers to how to achieve that, there is a possibly easier way in this case. A case insensitive search is sufficient:

import re

text = "Practice changing the Color"
words = {'color': 'colour', 'practice': 'practise'}

def replace(words,text):
        keys = words.keys()
        for i in keys:
                exp = re.compile(i, re.I)
                text = re.sub(exp, words[i], text)
        return text

text = replace(words,text)
print text
5
  • 3
    It's far better to use the built-in string methods than the regular expression module when the built-ins can easily handle it, which they can in this case.
    – John Y
    Commented Jan 17, 2010 at 19:44
  • thanks calmh. I'm short on time right now, so your quick and simple solution suits me nicely. thanks
    – Kim
    Commented Jan 17, 2010 at 19:54
  • @John Y: What would be the regexp-less solution to this? I don't see it.
    – Jakob Borg
    Commented Jan 17, 2010 at 19:57
  • Kim already mentioned it: use the string.lower() method. Other answers also mentioned it. Comments are no good for posting code, so I guess I will post my own answer.
    – John Y
    Commented Jan 18, 2010 at 5:33
  • +1 This solution worked best for me, since in my case, the case of the dictionary key matters, and simply lowercasing the key on set is not sufficient.
    – yobiscus
    Commented May 13, 2015 at 14:22
2

If you only need to do this once in your code (hence, no point to a function), the most straightforward way to deal with the problem is this:

lowercase_dict = {key.lower(): value for (key, value) in original_dict}

I'm assuming here that the dict in question isn't all that large--it might be inelegant to duplicate it, but if it's not large, it isn't going to hurt anything.

The advantage of this over @Fred's answer (though that also works) is that it produces the same result as a dict when the key isn't present: a KeyError.

0

There are multiple approaches to this problem, each has its set of pros and cons. Just to add to the list (looks like this option wasn't mentioned), it's possible to extend str class and use it as a key:

class CaseInsensitiveStr(str):
    def __hash__(self) -> 'int':
        return hash(self.lower())
    def __eq__(self, other:'str') -> 'bool':
        return self.lower() == other.lower()

It can work well if dictionary in question is private and some kind of interface is used to access it.

class MyThing:
    def __init__(self):
        self._d: 'dict[CaseInsensitiveStr, int]' = dict()
    def set(self, key:'str', value:'int'):
        self._d[CaseInsensitiveStr(key)] = value
    def get(self, key:'str') -> 'int':
        return self._d[CaseInsensitiveStr(key)]
0

Credit: based on @m000's answer. The following variant provides a get_orig_key method, by keeping track of the case-sensitive key of the last "set" operation.

class RobbieCaseInsensitiveDict(dict):
    @classmethod
    def _k(cls, key):
        return key.lower() if isinstance(key, str) else key

    def __init__(self, *args, **kwargs):
        super(RobbieCaseInsensitiveDict, self).__init__(*args, **kwargs)
        self.key_dict = {}
        for key in self.keys():
            if isinstance(key, str):
                self.key_dict[key.lower()] = key
        self._convert_keys()

    def get_orig_key(self, case_ins_key):
        if case_ins_key in self.key_dict:
            return self.key_dict[case_ins_key]
        else:
            return case_ins_key

    def __getitem__(self, key):
        return super(RobbieCaseInsensitiveDict, self).__getitem__(self.__class__._k(key))

    def __setitem__(self, key, value):
        if isinstance(key, str):
            self.key_dict[key.lower()] = key
        super(RobbieCaseInsensitiveDict, self).__setitem__(self.__class__._k(key), value)

    def __delitem__(self, key):
        return super(RobbieCaseInsensitiveDict, self).__delitem__(self.__class__._k(key))

    def __contains__(self, key):
        return super(RobbieCaseInsensitiveDict, self).__contains__(self.__class__._k(key))

    def has_key(self, key):
        return super(RobbieCaseInsensitiveDict, self).has_key(self.__class__._k(key))

    def pop(self, key, *args, **kwargs):
        return super(RobbieCaseInsensitiveDict, self).pop(self.__class__._k(key), *args, **kwargs)

    def get(self, key, *args, **kwargs):
        return super(RobbieCaseInsensitiveDict, self).get(self.__class__._k(key), *args, **kwargs)

    def setdefault(self, key, *args, **kwargs):
        if isintance(key, str):
            self.key_dict[key.lower()] = key
        return super(RobbieCaseInsensitiveDict, self).setdefault(self.__class__._k(key), *args, **kwargs)

    def update(self, E={}, **F):
        super(RobbieCaseInsensitiveDict, self).update(self.__class__(E))
        super(RobbieCaseInsensitiveDict, self).update(self.__class__(**F))

    def _convert_keys(self):
        for k in list(self.keys()):
            v = super(RobbieCaseInsensitiveDict, self).pop(k)
            self.__setitem__(k, v)
0

Here's a simple and correct implementation compatible with Python 3. Hopefully also simple enough to be understood by humans. Other answers still try to be compatible with Python 2.

from collections.abc import Mapping


class CaseInsensitiveDict(dict):

    def __init__(self, data=None, **kwargs):
        self.update(data, **kwargs)

    def get(self, k, default=None):
        return super().get(k.lower(), default)

    def pop(self, k):
        return super().pop(k.lower())

    def setdefault(self, k, default=None):
        return super().setdefault(k.lower(), default)

    def update(self, data=None, **kwargs):
        if data is not None:
            if isinstance(data, Mapping):
                data = data.items()
            super().update((k.lower(), v) for k, v in data)
        super().update((k.lower(), v) for k, v in kwargs)

    def __contains__(self, k):
        return super().__contains__(k.lower())

    def __delitem__(self, k):
        super().__delitem__(k.lower())

    def __eq__(self, other):
        if not isinstance(other, Mapping):
            return False
        if not isinstance(other, CaseInsensitiveDict):
            other = CaseInsensitiveDict(other)
        return super().__eq__(other)

    def __getitem__(self, k):
        return super().__getitem__(k.lower())

    def __setitem__(self, k, v):
        super().__setitem__(k.lower(), v)
-1

I just set up a function to handle this:

def setLCdict(d, k, v):
    k = k.lower()
    d[k] = v
    return d

myDict = {}

So instead of

myDict['A'] = 1
myDict['B'] = 2

You can:

myDict = setLCdict(myDict, 'A', 1)
myDict = setLCdict(myDict, 'B', 2)

You can then either lower case the value before looking it up or write a function to do so.

    def lookupLCdict(d, k):
        k = k.lower()
        return d[k]

    myVal = lookupLCdict(myDict, 'a')

Probably not ideal if you want to do this globally but works well if its just a subset you wish to use it for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.