Using a Python Dictionary as a Key (Non-nested)
data-structures, dictionary, python
Solution
If you have a really immutable dictionary (although it isn't clear to me why you don't just use a list of pairs: e.g. `[('content-type', 'text/plain'), ('host', 'example.com')]`), then you may convert your `dict` into:
A tuple of pairs. You've already done that in your question. A `tuple` is required instead of `list` because the results rely on the ordering and the immutability of the elements.
>>> tuple(sorted(a.items()))
A frozen set. It is a more suitable approach from the mathematical point of view, as it requires only the equality relation on the elements of your immutable `dict`, while the first approach requires the ordering relation besides equality.
>>> frozenset(a.items())
Problem
Python doesn't allow dictionaries to be used as keys in other dictionaries. Is there a workaround for using non-nested dictionaries as keys? The general problem with more complicated non-hashable objects and my specific use case has been moved here. My original description of my use case was incorrect.