multidict

6.7.1 · active · verified Sat Mar 28

multidict is a mapping implementation that allows multiple values per key, mirroring the semantics of HTTP headers and query strings. It provides four concrete classes — MultiDict (mutable), MultiDictProxy (immutable view), CIMultiDict (case-insensitive mutable), and CIMultiDictProxy (case-insensitive immutable view) — plus the istr helper for pre-folded string keys. Current version is 6.7.1 (released January 2026). The library ships an optional C extension; release cadence is frequent patch releases with periodic minor releases adding features such as the new merge() method (6.6.0) and a hash-table backend replacing the previous O(N) array (6.5.0).

Warnings

Install

Imports

Quickstart

Demonstrates mutable MultiDict with duplicate keys, getall, add, and a case-insensitive CIMultiDict.

from multidict import MultiDict, MultiDictProxy, CIMultiDict, istr

# --- Mutable multi-valued dict ---
d = MultiDict([('a', 1), ('b', 2), ('a', 3)])
assert d['a'] == 1              # returns FIRST value only
assert d.getall('a') == [1, 3] # all values for key
d.add('b', 99)                  # append without replacing
assert len(d) == 4

# --- set() replaces ALL entries for that key ---
d['a'] = 99
assert d.getall('a') == [99]

# --- Read-only proxy (dynamic view) ---
proxy = MultiDictProxy(d)
assert proxy['b'] == 2
d.add('b', 42)
assert proxy.getall('b') == [2, 99, 42]  # proxy reflects changes

# --- Case-insensitive dict ---
ci = CIMultiDict(Content_Type='application/json')
assert 'content-type' in ci
assert ci['CONTENT-TYPE'] == 'application/json'

# --- istr for pre-folded keys (performance) ---
HDR = istr('Content-Type')
assert ci[HDR] == 'application/json'

# --- merge (6.6+): copies key only if not already present ---
base = MultiDict(a=1)
base.merge(MultiDict(a=99, b=2))
assert base['a'] == 1   # not overwritten
assert base['b'] == 2

view raw JSON →