Bidirectional Mapping Library

0.23.1 · active · verified Sun Mar 29

The bidict library for Python provides efficient and Pythonic data structures for working with bidirectional (one-to-one) mappings. It offers familiar dictionary-like APIs, automatically keeping forward and inverse mappings in sync. The library is mature, actively maintained, and currently at version 0.23.1, with a regular release cadence. [1, 3, 10]

Warnings

Install

Imports

Quickstart

This example demonstrates creating a basic bidirectional mapping, accessing forward and inverse entries, and observing how updates are automatically synchronized. [3, 10]

from bidict import bidict

element_by_symbol = bidict({'H': 'hydrogen'})
print(f"Forward mapping: {element_by_symbol['H']}")
print(f"Inverse mapping: {element_by_symbol.inverse['hydrogen']}")

# Updates are automatically synchronized in both directions
element_by_symbol['H'] = 'hydrogène'
print(f"Updated forward mapping: {element_by_symbol['H']}")
print(f"Updated inverse mapping: {element_by_symbol.inverse['hydrogène']}")

view raw JSON →