This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Add "bidimap" to collections library: a simple bidirectional map
Type: performance Stage: resolved
Components: Versions: Python 3.10
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: jneb, rhettinger, serhiy.storchaka, veky
Priority: normal Keywords:

Created on 2021-08-17 06:53 by jneb, last changed 2022-04-11 14:59 by admin. This issue is now closed.

File name Uploaded Description Edit jneb, 2021-08-17 06:53 bidimap: a bidirectional map
Repositories containing patches
Messages (6)
msg399710 - (view) Author: Jurjen N.E. Bos (jneb) * Date: 2021-08-17 06:53
The Java class "BiDiMap" is very useful and doesn't seem to have an equivalent in the Python libraries.
I wrote a proposed class that does just that.
Here's a simple implementation, that could be used as a starting point.
msg399711 - (view) Author: Jurjen N.E. Bos (jneb) * Date: 2021-08-17 06:56
Give me a shout if you like this: I am happy to write a test suite, make a patch, etc.
msg399712 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-08-17 07:02
Is BiDiMap included in the Java SDK?
msg399714 - (view) Author: Jurjen N.E. Bos (jneb) * Date: 2021-08-17 07:12
It is part of the Apache Common collections
msg399718 - (view) Author: Vedran Čačić (veky) * Date: 2021-08-17 08:45
Your implementation has many problems. First, almost all your complexity claims are wrong, probably because you copied them from Java, which uses balanced trees instead of Python's hash tables.
(Also, the method names seem to be copied from Java, using camelCase which is unusual for Python stdlib.)

Second, shouldn't _inverse be a weakkey dictionary? Otherwise many operations (e.g. removeValue) will get two dicts out of sync quickly.
msg399751 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-08-17 13:58
This has come up several times in the past and never moved forward.  Part of the reason is that the mapping API doesn't translate cleanly to bidirectional lookups and it leaves users trapped if two keys every end up needing to be mapped to the same value (i.e. libre->free and gratis->free). Mostly, folks are better-off with two separate dictionaries augmented by a single function to create the initial pairing.  Also note that there are several bimaps published on PyPi but they have very low uptake.

If you want to pursue this further, I suggest bringing this to python-ideas (but first scan the archives for previous discussions and scan github for cases where projects have used one of the existing implementations).
Date User Action Args
2022-04-11 14:59:48adminsetgithub: 89094
2021-08-17 13:58:51rhettingersetstatus: open -> closed

nosy: + rhettinger
messages: + msg399751

resolution: rejected
stage: resolved
2021-08-17 08:45:06vekysetnosy: + veky
messages: + msg399718
2021-08-17 07:12:43jnebsetmessages: + msg399714
2021-08-17 07:02:14serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg399712
2021-08-17 06:57:29jnebsettitle: Add "bidimap" to collections library -> Add "bidimap" to collections library: a simple bidirectional map
2021-08-17 06:56:59jnebsetmessages: + msg399711
2021-08-17 06:54:57jnebsettype: performance
versions: + Python 3.10
2021-08-17 06:53:46jnebcreate