I'm currently in the need for a Python container class with similar functionality like the builtin dict
type. Basically what I need is a dictionary, where an arbitrary number of keys beside a primary key, which map to the very same value. However when iterating over it, it should iterate only over the (primary_key, value)
pairs and only the primary key if the list of keys is requested.
If this has already been implemented I'd rather not reinvent the wheel. So is there already a module providing such a container? If not, I'm going to implement it myself.
Here is a quick implementation:
The only messy bit is that it has to search the whole dict in case a primary key gets deleted.
I omitted
copy()
,pop()
,popitem()
andsetdefault()
. If you need them, you'll have to implement them yourself.