Skip to main content

MultiValueDict

A subclass of dictionary customized to handle multiple values for the same key.

Constructor

Signature

def MultiValueDict(
key_to_list_mapping: tuple|dict = ()
)

Parameters

NameTypeDescription
key_to_list_mapping`tupledict` = ()

Methods


get()

@classmethod
def get(
key: any,
default: any = None
) - > any

Return the last data value for the passed key. If key doesn't exist or value is an empty list, return default.

Parameters

NameTypeDescription
keyanyThe key to retrieve.
defaultany = NoneThe value to return if the key is not found or has no values.

Returns

TypeDescription
anyThe last value in the list for the key, or the default value if the key is missing or empty.

getlist()

@classmethod
def getlist(
key: any,
default: any = None
) - > list

Return the list of values for the key. If key doesn't exist, return a default value.

Parameters

NameTypeDescription
keyanyThe key to look up.
defaultany = NoneThe value to return if the key is not found.

Returns

TypeDescription
listA copy of the list of values for the specified key.

setlist()

@classmethod
def setlist(
key: any,
list_: list
) - > null

Sets the entire list of values for a specific key.

Parameters

NameTypeDescription
keyanyThe key to update.
list_listThe list of values to associate with the key.

Returns

TypeDescription
nullNone

setdefault()

@classmethod
def setdefault(
key: any,
default: any = None
) - > any

Returns the value of the key if it exists; otherwise, sets the key to the default value and returns it.

Parameters

NameTypeDescription
keyanyThe key to check or set.
defaultany = NoneThe value to set if the key is missing.

Returns

TypeDescription
anyThe value associated with the key after the operation.

setlistdefault()

@classmethod
def setlistdefault(
key: any,
default_list: list = None
) - > list

Returns the list of values for a key; if the key is missing, it initializes the key with the provided default list.

Parameters

NameTypeDescription
keyanyThe key to check or set.
default_listlist = NoneThe list to use as a default if the key is missing.

Returns

TypeDescription
listThe list of values associated with the key.

appendlist()

@classmethod
def appendlist(
key: any,
value: any
) - > null

Append an item to the internal list associated with key.

Parameters

NameTypeDescription
keyanyThe key whose list should be appended to.
valueanyThe value to add to the end of the key's list.

Returns

TypeDescription
nullNone

items()

@classmethod
def items() - > generator

Yield (key, value) pairs, where value is the last item in the list associated with the key.

Returns

TypeDescription
generatorA generator yielding tuples of keys and their respective last values.

lists()

@classmethod
def lists() - > iterator

Yield (key, list) pairs.

Returns

TypeDescription
iteratorAn iterator yielding tuples of keys and their full list of values.

values()

@classmethod
def values() - > generator

Yield the last value on every key list.

Returns

TypeDescription
generatorA generator yielding the final element from each key's value list.

copy()

@classmethod
def copy() - > [MultiValueDict](multivaluedict.md?sid=django_utils_datastructures_multivaluedict)

Return a shallow copy of this object.

Returns

TypeDescription
[MultiValueDict](multivaluedict.md?sid=django_utils_datastructures_multivaluedict)A new instance of MultiValueDict containing the same data.

update()

@classmethod
def update(
args: any,
kwargs: any
) - > null

Extend rather than replace existing key lists.

Parameters

NameTypeDescription
argsanyA mapping or iterable of tuples containing keys and values to add.
kwargsanyKey-value pairs to add to the dictionary.

Returns

TypeDescription
nullNone

dict()

@classmethod
def dict() - > dict

Return current object as a dict with singular values.

Returns

TypeDescription
dictA standard dictionary where each key maps only to its last value.