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]
pip install bidictVerified import paths — ran on the pinned version, not inferred.
This example demonstrates creating a basic bidirectional mapping, accessing forward and inverse entries, and observing how updates are automatically synchronized. [3, 10]
Replace `.inv` with `.inverse`.
Upgrade to Python 3.8+ (recommended `bidict` requirement) or ensure your `bidict` version is pinned to <0.22.0.
Consult the changelog for `bidict` 0.20.0 for alternative approaches to handling duplicate key/value scenarios. [2]
Ensure that any values you intend to store in a `bidict` are immutable and hashable (e.g., strings, numbers, tuples). [6]
Use `my_ordered_bidict.equals_order_sensitive(other_bidict)` for order-aware comparisons. [9, 14]
Always use `bidict` for one-to-one bidirectional mappings to ensure consistency and correctness. [1, 4, 6]
Use `import importlib.metadata; importlib.metadata.version('bidict')` or `bidict.__version__` to get the version string. [2]Install the library using pip: `pip install bidict`
Ensure all values inserted into a `bidict` are unique. If overwriting is desired, use `forceput()` or specify an appropriate `OnDupAction` with `put()`, for example: `b.forceput('new_key', 'existing_value')` or `b.put('new_key', 'existing_value', on_dup=bidict.OnDup(val=bidict.ON_DUP_DROP_OLD))`.Use hashable types for values, such as tuples, frozensets, strings, numbers, or custom objects that implement `__hash__` and `__eq__` methods. For example, replace `['opt', 'pot', 'top']` with `('opt', 'pot', 'top')`.Use the mutable `bidict` type if you need to modify the mapping after creation, or create a new `frozenbidict` with the desired changes.
Update your code to use the modern `OnDupAction` enum or its predefined instances. For example, replace `bidict.OVERWRITE` with `bidict.ON_DUP_DROP_OLD` when using methods like `put()` or `putall()`.
No dependency data recorded yet.