Skip to content

Instantly share code, notes, and snippets.

@babakness
Forked from bloomonkey/caselessDictionary.py
Created October 16, 2012 18:46
Show Gist options
  • Save babakness/3901174 to your computer and use it in GitHub Desktop.
Save babakness/3901174 to your computer and use it in GitHub Desktop.
A Python dictionary sub-class that is case-insensitive when searching, but also preserves the keys as inserted.
class CaselessDictionary(dict):
"""Dictionary that enables case insensitive searching while preserving case sensitivity
when keys are listed, ie, via keys() or items() methods.
Works by storing a lowercase version of the key as the new key and stores the original key-value
pair as the key's value (values become dictionaries)."""
def __init__(self, initval={}):
if isinstance(initval, dict):
for key, value in initval.iteritems():
self.__setitem__(key, value)
elif isinstance(initval, list):
for (key, value) in initval:
self.__setitem__(key, value)
def __contains__(self, key):
return dict.__contains__(self, key.lower())
def __getitem__(self, key):
return dict.__getitem__(self, key.lower())['val']
def __setitem__(self, key, value):
return dict.__setitem__(self, key.lower(), {'key': key, 'val': value})
def get(self, key, default=None):
try:
v = dict.__getitem__(self, key.lower())
except KeyError:
return default
else:
return v['val']
def has_key(self,key):
if self.get(key):
return True
else:
return False
def items(self):
return [(v['key'], v['val']) for v in dict.itervalues(self)]
def keys(self):
return [v['key'] for v in dict.itervalues(self)]
def values(self):
return [v['val'] for v in dict.itervalues(self)]
def iteritems(self):
for v in dict.itervalues(self):
yield v['key'], v['val']
def iterkeys(self):
for v in dict.itervalues(self):
yield v['key']
def itervalues(self):
for v in dict.itervalues(self):
yield v['val']
@sziring
Copy link

sziring commented Mar 11, 2016

This worked great for my acronym lookup program, thanks!

@Utumno
Copy link

Utumno commented Mar 30, 2017

d=CaselessDictionary({'lol':''})
print d.has_key('lol') # False

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment