boltons

25.0.0 · active · verified Mon Apr 06

boltons is a collection of over 250 pure-Python utilities designed to fill gaps in Python's standard library. It provides high-performance data structures and convenient functions for common tasks across various domains like file systems, iteration, caching, and time management. The library maintains an active development status, with major releases occurring roughly annually, interspersed with minor updates bringing enhancements and bug fixes. [1, 4, 5, 6, 8, 11]

Warnings

Install

Imports

Quickstart

This example demonstrates the `OrderedMultiDict` from `boltons.dictutils`, which allows storing multiple values for a single key while preserving insertion order. It's useful for scenarios where a standard dictionary's behavior is insufficient. [5, 6]

from boltons.dictutils import OrderedMultiDict

# Initialize the OrderedMultiDict
omd = OrderedMultiDict()

# Add items - supports multiple values for a single key
omd['fruit'] = 'apple'
omd['fruit'] = 'banana'
omd['vegetable'] = 'carrot'

# Access all values for a key
print(f"All fruits: {omd['fruit']}")
# Expected output: All fruits: ['apple', 'banana']

# Get the first value for a key
print(f"First fruit: {omd.get_first('fruit')}")
# Expected output: First fruit: apple

# Iterate through all key-value pairs (maintaining insertion order)
print("\nAll items:")
for key, value in omd.items():
    print(f"{key}: {value}")
# Expected output:
# All items:
# fruit: apple
# fruit: banana
# vegetable: carrot

view raw JSON →