[Python-ideas] Dict with inverse form

Andrew Barnert abarnert at yahoo.com
Thu Feb 11 23:07:47 EST 2016


On Thursday, February 11, 2016 6:15 PM, João Bernardo <jbvsmo at gmail.com> wrote:

>Many times I have the need for a dictionary and its inverse form (keys and values swapped). For static dictionaries, it is easy, but sometimes I wish there were a class to hold and update both dictionaries at the same time. Also it will avoid creating 2 variables. 


This really sounds like something you should just flesh out and put on PyPI, and then suggest for stdlib inclusion once it's stable. And not just for the usual reasons, but because I think there are some design decisions that you'll want to work through and then play with in real code before you're sure you're happy with it.

For example, when I've built something like this, sometimes I want errors on duplicate values, sometimes just pick one arbitrarily, sometimes I want the inverse dict to store sets of values, and sometimes I even want _both_ to store sets of values. Do you want to do just one of those, or two or more? Controlled by flags? Or different attribute names? Or different classes?


More information about the Python-ideas mailing list