How to make a dictionary that returns key for keys missing from the dictionary instead of raising KeyError?

I want to create a python dictionary that returns me the key value for the keys are missing from the dictionary.

Usage example:

dic = smart_dict()
dic['a'] = 'one a'
print(dic['a'])
# >>> one a
print(dic['b'])
# >>> b

Solution 1:

dicts have a __missing__ hook for this:

class smart_dict(dict):
    def __missing__(self, key):
        return key

Could simplify it as (since self is never used):

class smart_dict(dict):
    @staticmethod
    def __missing__(key):
        return key

Solution 2:

Why don't you just use

dic.get('b', 'b')

Sure, you can subclass dict as others point out, but I find it handy to remind myself every once in a while that get can have a default value!

If you want to have a go at the defaultdict, try this:

dic = defaultdict()
dic.__missing__ = lambda key: key
dic['b'] # should set dic['b'] to 'b' and return 'b'

except... well: AttributeError: ^collections.defaultdict^object attribute '__missing__' is read-only, so you will have to subclass:

from collections import defaultdict
class KeyDict(defaultdict):
    def __missing__(self, key):
        return key

d = KeyDict()
print d['b'] #prints 'b'
print d.keys() #prints []

Solution 3:

Congratulations. You too have discovered the uselessness of the standard collections.defaultdict type. If that execrable midden heap of code smell offends your delicate sensibilities as much as it did mine, this is your lucky StackOverflow day.

Thanks to the forbidden wonder of the 3-parameter variant of the type() builtin, crafting a non-useless default dictionary type is both fun and profitable.

What's Wrong with dict.__missing__()?

Absolutely nothing, assuming you like excess boilerplate and the shocking silliness of collections.defaultdict – which should behave as expected but really doesn't. To be fair, Jochen Ritzel's accepted solution of subclassing dict and implementing the optional __missing__() method is a fantastic workaround for small-scale use cases only requiring a single default dictionary.

But boilerplate of this sort scales poorly. If you find yourself instantiating multiple default dictionaries, each with their own slightly different logic for generating missing key-value pairs, an industrial-strength alternative automating boilerplate is warranted.

Or at least nice. Because why not fix what's broken?

Introducing DefaultDict

In less than ten lines of pure Python (excluding docstrings, comments, and whitespace), we now define a DefaultDict type initialized with a user-defined callable generating default values for missing keys. Whereas the callable passed to the standard collections.defaultdict type uselessly accepts no parameters, the callable passed to our DefaultDict type usefully accepts the following two parameters:

  1. The current instance of this dictionary.
  2. The current missing key to generate a default value for.

Given this type, solving sorin's question reduces to a single line of Python:

>>> dic = DefaultDict(lambda self, missing_key: missing_key)
>>> dic['a'] = 'one a'
>>> print(dic['a'])
one a
>>> print(dic['b'])
b

Sanity. At last.

Code or It Didn't Happen

def DefaultDict(keygen):
    '''
    Sane **default dictionary** (i.e., dictionary implicitly mapping a missing
    key to the value returned by a caller-defined callable passed both this
    dictionary and that key).

    The standard :class:`collections.defaultdict` class is sadly insane,
    requiring the caller-defined callable accept *no* arguments. This
    non-standard alternative requires this callable accept two arguments:

    #. The current instance of this dictionary.
    #. The current missing key to generate a default value for.

    Parameters
    ----------
    keygen : CallableTypes
        Callable (e.g., function, lambda, method) called to generate the default
        value for a "missing" (i.e., undefined) key on the first attempt to
        access that key, passed first this dictionary and then this key and
        returning this value. This callable should have a signature resembling:
        ``def keygen(self: DefaultDict, missing_key: object) -> object``.
        Equivalently, this callable should have the exact same signature as that
        of the optional :meth:`dict.__missing__` method.

    Returns
    ----------
    MappingType
        Empty default dictionary creating missing keys via this callable.
    '''

    # Global variable modified below.
    global _DEFAULT_DICT_ID

    # Unique classname suffixed by this identifier.
    default_dict_class_name = 'DefaultDict' + str(_DEFAULT_DICT_ID)

    # Increment this identifier to preserve uniqueness.
    _DEFAULT_DICT_ID += 1

    # Dynamically generated default dictionary class specific to this callable.
    default_dict_class = type(
        default_dict_class_name, (dict,), {'__missing__': keygen,})

    # Instantiate and return the first and only instance of this class.
    return default_dict_class()


_DEFAULT_DICT_ID = 0
'''
Unique arbitrary identifier with which to uniquify the classname of the next
:func:`DefaultDict`-derived type.
'''

The key ...get it, key? to this arcane wizardry is the call to the 3-parameter variant of the type() builtin:

type(default_dict_class_name, (dict,), {'__missing__': keygen,})

This single line dynamically generates a new dict subclass aliasing the optional __missing__ method to the caller-defined callable. Note the distinct lack of boilerplate, reducing DefaultDict usage to a single line of Python.

Automation for the egregious win.

Solution 4:

The first respondent mentioned defaultdict, but you can define __missing__ for any subclass of dict:

>>> class Dict(dict):
        def __missing__(self, key):
            return key


>>> d = Dict(a=1, b=2)
>>> d['a']
1
>>> d['z']
'z'

Also, I like the second respondent's approach:

>>> d = dict(a=1, b=2)
>>> d.get('z', 'z')
'z'