Skip to main content

Fully generic MultiDict class

Project description

multicollections

A fully generic MultiDict class that allows multiple values for the same key while preserving insertion order.

Documentation CI Codecov Checked with mypy Ruff uv Publish PyPI PyPI - Python Version

✨ Features

  • 🔑 Multiple values per key: Store multiple values for the same key, perfect for handling data like HTTP headers, form data, or configuration files
  • 📝 Insertion order preserved: Maintains the order in which items are added
  • 🧩 Fully generic: Accepts any types for both keys and values
  • ✅ Thoroughly tested: 100% code coverage
  • ⚡ Type-safe: Fully typed with generics
  • 🪶 Lightweight: Zero dependencies, pure Python implementation
  • 🎯 Rich, compatible API: Implements the multidict API
  • 📐 Abstract base classes: The multicollections.abc module provides a common interface for other custom multi-value collections

📦 Installation

pip

pip install multicollections

conda

conda install -c conda-forge multicollections

🚀 Quick Start

from multicollections import MultiDict

# Create a MultiDict with duplicate keys
headers = MultiDict([
    ('Accept', 'text/html'),
    ('Accept-Encoding', 'gzip'),
    ('Accept', 'application/json'),  # Same key, different value
    ('User-Agent', 'MyApp/1.0')
])

# Access the first value for a key
print(headers['Accept'])  # 'text/html'

# Get ALL values for a key
print(headers.getall('Accept'))  # ['text/html', 'application/json']

# See all key-value pairs (duplicates preserved)
print(list(headers.items()))
# [('Accept', 'text/html'), ('Accept-Encoding', 'gzip'), 
#  ('Accept', 'application/json'), ('User-Agent', 'MyApp/1.0')]

# Add more values for existing keys
headers.add('Accept', 'text/xml')
print(headers.getall('Accept'))  # ['text/html', 'application/json', 'text/xml']
print(len(headers))  # 5 items total

# Remove and return the first value
first_accept = headers.popone('Accept')
print(first_accept)  # 'text/html'
print(headers.getall('Accept'))  # ['application/json', 'text/xml']

# Remove and return all values for a key
all_accepts = headers.popall('Accept')
print(all_accepts)  # ['application/json', 'text/xml']
print('Accept' in headers)  # False

# Create from keyword arguments
config = MultiDict(host='localhost', port=8080, debug=True)

# Mix iterable and keyword arguments
mixed = MultiDict([('a', 1), ('b', 2)], c=3, d=4)

📖 Why MultiDict?

Standard Python dictionaries can only hold one value per key. When you need to handle data formats that naturally allow multiple values for the same key, MultiDict is the perfect solution:

  • HTTP headers: Multiple Accept or Set-Cookie headers
  • URL query parameters: ?tag=python&tag=web&tag=api
  • Form data: Multiple form fields with the same name
  • Configuration files: Multiple values for the same configuration key

As opposed to the popular multidict package, multicollections's MultiDict implementation allows both keys and values to be of any type, providing greater flexibility.

🔗 Documentation

For detailed documentation, examples, and API reference, visit: https://multicollections.readthedocs.io/

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

multicollections-1.0.5.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

multicollections-1.0.5-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file multicollections-1.0.5.tar.gz.

File metadata

  • Download URL: multicollections-1.0.5.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.8.17

File hashes

Hashes for multicollections-1.0.5.tar.gz
Algorithm Hash digest
SHA256 a58c28581dc2d981237c5a60d66028e6ef596858f75f2a3dcfb0091ebfe1c788
MD5 a275bfa2cd49e246011a90e83c2333ae
BLAKE2b-256 10650c8ad113380e4765f74d144547592b22a33363f3e1ed158d4ad441ac74d6

See more details on using hashes here.

File details

Details for the file multicollections-1.0.5-py3-none-any.whl.

File metadata

File hashes

Hashes for multicollections-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 3436a5532ec32ad8653764ce0827c67a66f92b287f6d178a53c5821ea23f6656
MD5 35f5bdba1511b9597afde244b6fdfb66
BLAKE2b-256 8f883ecbb049f7509d55f1cb4da673415248a6448cf2eea2b2bad68c0ada4c74

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page