dict
s 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
More Related Contents:
- How do I sort a list of dictionaries by a value of the dictionary?
- What is the best way to implement nested dictionaries?
- How are Python’s Built In Dictionaries Implemented?
- What would a “frozen dict” be?
- In Python, when to use a Dictionary, List or Set?
- merging Python dictionaries
- What is the purpose of collections.ChainMap?
- Key-ordered dict in Python
- Creating a tree/deeply nested dict from an indented text file in python
- Using a Python Dictionary as a Key (Non-nested)
- Recursive diff of two dictionaries (keys and values)?
- How expensive are Python dictionaries to handle?
- Adding counters deletes keys
- How to sort a list of dictionaries by a value of the dictionary in Python?
- How can I remove a key from a Python dictionary?
- Reverse / invert a dictionary mapping
- Python Sets vs Lists
- Dictionary: Get list of values for list of keys
- Create a list with initial capacity in Python
- How do you create nested dict in Python?
- Is there a clever way to pass the key to defaultdict’s default_factory?
- What do I use for a max-heap implementation in Python?
- Sorting a dictionary with lists as values, according to an element from the list
- Return first N key:value pairs from dict
- Merge several Python dictionaries
- Python: Elegantly merge dictionaries with sum() of values [duplicate]
- How to create a Python dictionary with double quotes as default quote format?
- Using Python class as a data container [closed]
- How can I convert string values from a dictionary, into int/float datatypes?
- sorting list of nested dictionaries in python