Quote

Python is basically dicts wrapped in loads of syntactic sugar.

‒ Lalo Martins

The dictionary data type is fundamental to Python’s implementation. Class and instance attributes, module namespaces, and function keyword arguments are among some of the core Python constructs represented by dictionaries in memory. The _builtins_._dict_ stores all built-in types, objects, and functions.

Dictionaries are unordered (dict is only theoretically unordered. There is the OrderedDict. Besides, dict is by default ordered since Python 3.6) collections of arbitrary types stored and retrieved by keys instead of positional offsets like the sequence types. The literal syntax for dictionaries is curly brackets {key1: value1, key2: value2, ...}. Like lists, dictionaries are mutable and arbitrarily nestable.

Python dictionaries are similar to associative arrays or hashes in other programming languages. Internally, Python dictionaries are implemented as hash tables optimised for speed and efficiency. That means any hashable object can be used as dictionary keys.

dict

dict does not raise an out-of-range or bound error when accessing (with the dict.get() method; otherwise, it will yield a KeyError) or setting values for non-existing keys, making it ideal for sparse data structures. It can even be used to simulate a ‘flexible list’ when integers are used as keys. Another common use case is to use tuples as keys to represent sparse matrices.

movies = {
    1975: 'Holy Grail', 
    1979: 'Life of Brian', 
    1983: 'The Meaning of Life'
}
movies[1979] # 'Life of Brian'
 
matrix = {}
matrix[(2, 3, 4)] = 88
matrix[(7, 8, 9)] = 99
 
x = 2; y = 3; z = 4;
matrix[(x, y, z)] # 88

Counter

Counter is a subclass of dic that stores the items as keys and their counts as values.

breakfast = collections.Counter(['egg', 'egg', 'bacon', 'sausage', 'toast])
breakfast['egg'] # 2
breakfast.most_common(2) # [('egg', 2), ('bacon', 1)]

defaultdict

defaultdict is a useful implementation of the Mapping abstract type that handles missing keys elegantly.

You can create similar behaviour by inheriting from dict or UserDict (UserDict is intended only as a base class to be extended and not a ‘ready-to-use’ type) and implementing the __missing__ special method. Although dict does not implement __missing__ itself, it is aware of it: d[k] will call the special method, but d.get(k) will raise a KeyError. For this reason, it is recommended to inherit from UserDict rather than dict.

OrderedDict

The dict type maintains the order of keys since Python 3.6, which renders OrderedDict less prominent, but not completely obsolete. Python documentation states that dict is good at mapping operations whilst OrderedDict is more efficient at handling frequent reordering operations.

ChainMap

ChainMap links multiple mappings so they can be queried as one - perfect for templating or simulating namespaces. It is significantly faster than creating a new dictionary and copying all input mappings into it using update.

Note: Updates and insertions affect only the first mapping in the chain