How to convert a MultiDict to nested dictionary using Python
Last Updated :
28 Apr, 2025
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.
Similar Reads
Convert Lists to Nested Dictionary - Python The task of converting lists to a nested dictionary in Python involves mapping elements from multiple lists into key-value pairs, where each key is associated with a nested dictionary. For example, given the lists a = ["gfg", "is", "best"], b = ["ratings", "price", "score"], and c = [5, 6, 7], the g
3 min read
How to convert NumPy array to dictionary in Python? The following article explains how to convert numpy array to dictionary in Python. Array in Numpy is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. In Numpy, number of dimensions of the array is called rank of the array. A tuple of integers givi
3 min read
Create a Nested Dictionary from Text File Using Python We are given a text file and our task is to create a nested dictionary using Python. In this article, we will see how we can create a nested dictionary from a text file in Python using different approaches. Create a Nested Dictionary from Text File Using PythonBelow are the ways to Create Nested Dic
3 min read
How To Convert Python Dictionary To JSON? In Python, a dictionary stores information using key-value pairs. But if we want to save this data to a file, share it with others, or send it over the internet then we need to convert it into a format that computers can easily understand. JSON (JavaScript Object Notation) is a simple format used fo
6 min read
Convert a list of Tuples into Dictionary - Python Converting a list of tuples into a dictionary involves transforming each tuple, where the first element serves as the key and the second as the corresponding value. For example, given a list of tuples a = [("a", 1), ("b", 2), ("c", 3)], we need to convert it into a dictionary. Since each key-value p
3 min read