werkzeug.MultiDict

class werkzeug.MultiDict(mapping=None)[source]

A MultiDict is a dictionary subclass customized to deal with multiple values for the same key which is for example used by the parsing functions in the wrappers. This is necessary because some HTML form elements pass multiple values for the same key.

MultiDict implements all standard dictionary methods. Internally, it saves all values for a key as a list, but the standard dict access methods will only return the first value for a key. If you want to gain access to the other values, too, you have to use the list methods as explained below.

Basic Usage:

>>> d = MultiDict([('a', 'b'), ('a', 'c')])
>>> d
MultiDict([('a', 'b'), ('a', 'c')])
>>> d['a']
'b'
>>> d.getlist('a')
['b', 'c']
>>> 'a' in d
True

It behaves like a normal dict thus all dict functions will only return the first value when multiple values for one key are found.

From Werkzeug 0.3 onwards, the KeyError raised by this class is also a subclass of the BadRequest HTTP exception and will render a page for a 400 BAD REQUEST if caught in a catch-all for HTTP exceptions.

A MultiDict can be constructed from an iterable of (key, value) tuples, a dict, a MultiDict or from Werkzeug 0.2 onwards some keyword parameters.

Parameters:mapping – the initial value for the MultiDict. Either a regular dict, an iterable of (key, value) tuples or None.

Methods

__init__([mapping])
add(key, value) Adds a new value for the key.
clear(() -> None.  Remove all items from D.)
copy() Return a shallow copy of this object.
deepcopy([memo]) Return a deep copy of this object.
fromkeys(...) v defaults to None.
get(key[, default, type]) Return the default value if the requested data doesn’t exist.
getlist(key[, type]) Return the list of items for a given key.
has_key((k) -> True if D has a key k, else False)
items(*a, **kw) Like iteritems(), but returns a list.
iteritems([multi]) Return an iterator of (key, value) pairs.
iterkeys()
iterlists() Return a list of (key, values) pairs, where values is the list of all values associated with the key.
iterlistvalues() Return an iterator of all values associated with a key.
itervalues() Returns an iterator of the first value on every key’s value list.
keys(*a, **kw) Like iterkeys(), but returns a list.
lists(*a, **kw) Like iterlists(), but returns a list.
listvalues(*a, **kw) Like iterlistvalues(), but returns a list.
pop(key[, default]) Pop the first item for a list on the dict.
popitem() Pop an item from the dict.
popitemlist() Pop a (key, list) tuple from the dict.
poplist(key) Pop the list for a key from the dict.
setdefault(key[, default]) Returns the value for the key if it is in the dict, otherwise it returns default and sets that value for key.
setlist(key, new_list) Remove the old values for a key and add new ones.
setlistdefault(key[, default_list]) Like setdefault but sets multiple values.
to_dict([flat]) Return the contents as regular dict.
update(other_dict) update() extends rather than replaces existing key lists:
values(*a, **kw) Like itervalues(), but returns a list.
viewitems(...)
viewkeys(...)
viewvalues(...)