Javatpoint Logo
Javatpoint Logo

How to Convert a MultiDict to Nested Dictionary using Python

Introduction:

This tutorial discusses how to convert a MultiDict to nested dictionary using Python. A MultiDict is a dictionary-like object that contains multiple values for the same key, making it a proper data structure for working with form and query strings. It is a subclass of Python's built-in dictionary and behaves the same. In some use cases, we may need to convert a MultiDict to a nested dictionary, where each key corresponds to a dictionary of values. This article will discuss the steps necessary to convert a MultiDict to a nested dictionary in Python. First, install the multidict library by typing the following command on the command line or terminal:

Python presents some other compound facts known as a dictionary, just like a listing in that it is miles a set of gadgets. You will cover simple Python dictionary functionality and learn how to get the correct entry to and manage dictionary facts. After finishing this education, you need to understand when a dictionary is the ideal record type and how to use it.

What are the steps to Convert MultiDict to a Nested Dictionary?

The steps to convert MultiDict to a nested dictionary are given below -

  1. Import multidict library.
  2. Create a MultiDict with the use of the MultiDict() constructor.
  3. Iterate over the objects within the MultiDict.
  4. Look at whether the vital thing already exists within the nested dictionary for every object.
  5. If the key exists, append the price to list values related to the important thing.
  6. If the key no longer exists, create a brand-new entry inside the nested dictionary with the key and a list of values containing the cost from the MultiDict.

What is Assigning Keys?

With this technique, we can create a brand-new empty dictionary. We then assigned the given dictionaries to every new key. The ensuing dictionary will be a nested dictionary with the keys assigned.

Example 1:

Here we are given an example of assigning keys in Python. Then the example is given below -

Output:

Now we compile the above code, and after successful compilation, we run it. Then the result is given below -

DictA: {'Rose': 1, 'Lotus': 2}
DictB: {'Tulip': 3, 'Rose': 5}
Netsed Dictionary:
{'Netsed_dict_1': {'Rose': 1, 'Lotus': 2}, 'Netsed_dict_2': {'Tulip': 3, 'Rose': 5}}

MultiDict to a Nested dictionary:

Example 1:

Here we are giving an example of converting multidict to a nested dictionary in Python. Now the example is given below -

Output:

In this situation, Create a MultiDict with keys' key1' and 'key2' and multiple values. Iter over the objects, upload value to the prevailing key's listing or create a new access with key and price inside the nested dictionary. Output is a nested dictionary with keys and lists of values related to every. Now we compile the above code, and after successful compilation, we run it. Then the result is given below -

{'key1': ['val1', 'val3'], 'key2': ['val2']}

Example 2:

Here we give another example of converting multidict to a nested dictionary in Python. Now the example is given below -

Output:

In this example, a MultiDict contains two keys, "fruit" and "color," with several values. Iterate through the elements and, if the key exists, add its value to the list; otherwise, create a new entry in the nested dictionary using the key and value lists. The final output is a nested dictionary of keys and a list of values associated with each key. Now we compile the above code, and after successful compilation, we run it. Then the result is given below -

{'fruit': ['Lichi', 'Mango'], 'color': ['Black', 'Blue']}






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA