Shorthand syntax for building OrderedDicts
od is a lightweight Python library (version 2.0.2) that provides a shorthand syntax for creating `collections.OrderedDict` instances. It aims to simplify the creation of ordered dictionaries with a concise and readable approach, leveraging Python's flexible syntax to make ordered dictionary construction more intuitive. The library is actively maintained.
Warnings
- gotcha For Python 3.7 and later, standard `dict` objects are guaranteed to preserve insertion order. This means that for many common use cases where `OrderedDict` was previously essential, a regular `dict` might now suffice and offer better performance or simpler code. Users should evaluate if they genuinely require `collections.OrderedDict`'s specific behaviors (like `move_to_end` or `popitem`'s LIFO/FIFO behavior) or if a standard `dict` meets their needs.
- gotcha The `od` library provides a convenient shorthand for *constructing* `collections.OrderedDict` instances. It does not replace or extend the full `OrderedDict` API. If you need to use specific `OrderedDict` methods (e.g., `move_to_end()`, `popitem(last=False)`), you will still interact directly with the `collections.OrderedDict` object returned by `od`.
Install
-
pip install od
Imports
- od
from od import od
Quickstart
from od import od
# Create an OrderedDict using shorthand syntax
my_ordered_dict = od({
'first_key': 1,
'second_key': 2,
'third_key': 3
})
print(my_ordered_dict)
print(type(my_ordered_dict))
# Verification of order
assert list(my_ordered_dict.keys()) == ['first_key', 'second_key', 'third_key']