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 Ruff ty 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.7.tar.gz (11.4 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.7-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: multicollections-1.0.7.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.11 {"installer":{"name":"uv","version":"0.9.11"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for multicollections-1.0.7.tar.gz
Algorithm Hash digest
SHA256 db800c8f757679849bcdff871666c46a06dbe514b3fa07c019a8ba04e52dce74
MD5 cd3dd15ce6df8b2552d58566db44cc2e
BLAKE2b-256 7158a3058549ad8453537da5abd8733c8f3b40cd4d1dbfaa929a6e3b852e5b02

See more details on using hashes here.

File details

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

File metadata

  • Download URL: multicollections-1.0.7-py3-none-any.whl
  • Upload date:
  • Size: 13.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.11 {"installer":{"name":"uv","version":"0.9.11"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for multicollections-1.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 56b30f8c9f61a1d5a6cc83df2823d871316df5b1ad47a057c50b54441a0be4b4
MD5 961bf6b704203b84cb8c7e5ac55927e3
BLAKE2b-256 4a157b36dc2369b9491cc277113d6a121e76eaa33c62f393d49e4809424e10b4

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