How to convert a MultiDict to nested dictionary using Python Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report 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 libraryCreate a MultiDict using the MultiDict() constructorIterate over the items in the MultiDictFor 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. Â Comment More infoAdvertise with us Next Article How to convert a MultiDict to nested dictionary using Python mukulsomukesh Follow Improve Article Tags : Python Practice Tags : python 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 Convert nested Python dictionary to object Let us see how to convert a given nested dictionary into an object Method 1 : Using the json module. We can solve this particular problem by importing the json module and use a custom object hook in the json.loads() method. python3 # importing the module import json # declaringa a class class obj: # 2 min read How to Create a Dictionary in Python The task of creating a dictionary in Python involves storing key-value pairs in a structured and efficient manner, enabling quick lookups and modifications. A dictionary is an unordered, mutable data structure where each key must be unique and immutable, while values can be of any data type. For exa 3 min read How To Convert Pandas Dataframe To Nested Dictionary In this article, we will learn how to convert Pandas DataFrame to Nested Dictionary. Convert Pandas Dataframe To Nested DictionaryConverting a Pandas DataFrame to a nested dictionary involves organizing the data in a hierarchical structure based on specific columns. In Python's Pandas library, we ca 2 min read Convert Unicode String to Dictionary in Python Python's versatility shines in its ability to handle diverse data types, with Unicode strings playing a crucial role in managing text data spanning multiple languages and scripts. When faced with a Unicode string and the need to organize it for effective data manipulation, the common task is convert 2 min read Python - Converting list string to dictionary Converting a list string to a dictionary in Python involves mapping elements from the list to key-value pairs. A common approach is pairing consecutive elements, where one element becomes the key and the next becomes the value. This results in a dictionary where each pair is represented as a key-val 3 min read Like