Registry / data / bidict

bidict

JSON →
library0.23.1pypypi✓ verified 52d ago

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]

dataserialization
pip install bidict
Install & Compatibility
Where this runs
tested against v0.23.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.925 runs
installs and imports cleanly · install 0.0s · import 0.021s · 18MB
glibc
py 3.103.925 runs
installs and imports cleanly · install 1.6s · import 0.018s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

bidict
from bidict import bidict
bidict.inv
my_bidict.inverse
my_bidict.inv
The `.inv` attribute was renamed to `.inverse` in v0.18.0. `inv` became an alias but `inverse` is the canonical and recommended name. [2, 5]

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']}")
Debug
Known issues
breakingThe `.inv` attribute for accessing the inverse mapping was renamed to `.inverse`. While `.inv` was an alias for a period, it's recommended to use `.inverse` for clarity and future compatibility.
fix
Replace `.inv` with `.inverse`.
affects: <0.18.0
breakingSupport for Python 3.6 was dropped. If you are using bidict with Python 3.6, you must remain on an older version of the library or upgrade your Python interpreter.
fix
Upgrade to Python 3.8+ (recommended `bidict` requirement) or ensure your `bidict` version is pinned to <0.22.0.
affects: >=0.22.0
breakingSeveral old APIs, including the `on_dup_key`, `on_dup_val`, and `on_dup_kv` arguments to `put()` and `putall()`, as well as the constants `bidict.OVERWRITE` and `bidict.IGNORE`, were removed.
fix
Consult the changelog for `bidict` 0.20.0 for alternative approaches to handling duplicate key/value scenarios. [2]
affects: >=0.20.0
gotchaUnlike standard `dict` values, values stored in a `bidict` must be hashable. This is because values serve as keys in the inverse mapping, and keys must always be hashable in Python dictionaries.
fix
Ensure that any values you intend to store in a `bidict` are immutable and hashable (e.g., strings, numbers, tuples). [6]
affects: All versions
gotchaFor `OrderedBidict`, the `==` operator (`__eq__`) performs an order-insensitive comparison, similar to regular `dict`s. If you require order-sensitive equality checking, use the `equals_order_sensitive()` method.
fix
Use `my_ordered_bidict.equals_order_sensitive(other_bidict)` for order-aware comparisons. [9, 14]
affects: All versions
gotchaAttempting to create a bidirectional mapping using two separate `dict`s or a single `dict` with duplicated key/value entries is error-prone and leads to manual synchronization issues and potential data inconsistency. `bidict` handles these invariants automatically.
fix
Always use `bidict` for one-to-one bidirectional mappings to ensure consistency and correctness. [1, 4, 6]
affects: All versions
deprecatedThe `bidict.__version_info__` attribute was removed.
fix
Use `import importlib.metadata; importlib.metadata.version('bidict')` or `bidict.__version__` to get the version string. [2]
affects: >=0.21.0
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'bidict'
The `bidict` library is not installed in the Python environment, or the Python interpreter being used cannot find it.
fix
Install the library using pip: `pip install bidict`
bidict.ValueDuplicationError: <value>
This error occurs when attempting to add an item to a `bidict` where the value already exists, violating the bidirectional uniqueness constraint.
fix
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))`.
TypeError: unhashable type: 'list'
Bidict requires both keys and *values* to be hashable because it maintains an inverse mapping where values become keys. Unhashable types like lists or dictionaries cannot be used as values.
fix
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')`.
TypeError: 'frozenbidict' object does not support item assignment
`frozenbidict` is an immutable version of `bidict`, meaning its contents cannot be changed after creation.
fix
Use the mutable `bidict` type if you need to modify the mapping after creation, or create a new `frozenbidict` with the desired changes.
AttributeError: module 'bidict' has no attribute 'OVERWRITE'
The API for handling duplication actions changed in `bidict`. Older constants like `OVERWRITE` have been replaced by the `OnDupAction` enum and related constants (e.g., `ON_DUP_DROP_OLD`, `ON_DUP_RAISE`).
fix
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()`.
Upgrade
Version history
0.23.1latest on PyPI
Audit
Dependencies

No dependency data recorded yet.

Agent activity
14 hits · last 30 days
seranking-bot
4
node
2
ahrefsbot
2
amazonbot
1
bytedance
1
Resources