How to convert a MultiDict to nested dictionary using Python
A MultiDict is a dictionary-like object that holds multiple values for the same key, making it a useful data structure for processing forms and query strings. It is a subclass of the Python built-in dictionary and behaves similarly. In some use cases, we may need to convert a MultiDict to a nested dictionary, where each key corresponds to a dictionary of values. In this article, we will discuss the steps required to convert a MultiDict to a nested dictionary in Python.
First of all, install multidict library by writing the following command in your command line or terminal:
pip install multidict
Steps to convert a MultiDict to a nested dictionary:
- Import multidict library
- Create a MultiDict using the MultiDict() constructor
- Iterate over the items in the MultiDict
- For each item, check if the key already exists in the nested dictionary.
- If the key exists, append the value to the list of values associated with the key.
- If the key does not exist, create a new entry in the nested dictionary with the key and a list of values containing the value from the MultiDict.
Example 1:
In the following example, we convert a MultiDict to a nested dictionary.
Python3
# import multidict from multidict import MultiDict # create a MultiDict data = MultiDict([( 'key1' , 'value1' ), ( 'key2' , 'value2' ), ( 'key1' , 'value3' )]) # initialize a nested dictionary nested_dict = {} # iterate over the items in the MultiDict for key, value in data.items(): # check if the key exists in the nested dictionary if key in nested_dict: # append the value to the list of values # associated with the key nested_dict[key].append(value) else : # create a new entry in the nested dictionary # with the key and a list of values nested_dict[key] = [value] # output the nested dictionary print (nested_dict) |
Output:
In this example, Create a MultiDict with keys ‘key1’ and ‘key2’ and multiple values. Iterate over the items, add value to the existing key’s list or create a new entry with key and value in the nested dictionary. Output is a nested dictionary with keys and lists of values associated with each.

Example 2:
In the following example, we convert a MultiDict to a nested dictionary.
Python3
# import multidict from multidict import MultiDict # create a MultiDict data = MultiDict([( 'fruit' , 'apple' ), ( 'color' , 'red' ), ( 'fruit' , 'banana' ), ( 'color' , 'yellow' )]) # initialize a nested dictionary nested_dict = {} # iterate over the items in the MultiDict for key, value in data.items(): # check if the key exists in the nested dictionary if key in nested_dict: # append the value to the list of values # associated with the key nested_dict[key].append(value) else : # create a new entry in the nested dictionary # with the key and a list of values nested_dict[key] = [value] # output the nested dictionary print (nested_dict) |
Output:
In this example, create a MultiDict with two keys ‘fruit’ and ‘color’ with multiple values. Iterate over the items, if the key exists, append its value to the list, else create a new entry in the nested dictionary with the key and list of values. The final output is a nested dictionary with keys and lists of values associated with each key.
