Add BiMap - a bidirectional dictionary to collections module

Actually, Scala’s BiMap is not a native type but a 3rd-party type (from Guava or Apache probably). AFAIK, other languages such as Rust, TypeScript or Perl do not support bidirectional mappings natively. So, I’m really not in favor of adding this pattern to the standard library.

Note that in order to have a bidirectional map, we need to restrict to hashable values (if you want O(1) lookup) and handle duplicated values as well. I suspect we’ll likely have just two internal mappings that do the trick and I don’t think it’s more efficient than having two independent dicts that you’ll update simultaneously (you can wrap that in a Mapping-interface with 2 internal dict attributes for instance).

It also feels intuitive to include it in the collections module, which already contains many specialized dictionary types

While they contain specialized dictionary types, they are here for efficiency and are mostly legacy content. So I doubt we’ll want to have more data structures like this.

If you want to add such type, I think a PEP is likely needed. Or we can have some recipe page for that (we have recipes for itertools, but having a recipe for collections could perhaps be a good start).

3 Likes