Perquisites : ImmutableMultiDict
In this article, we are going to use ImmutableMultiDict to extract the data using Python, which is a type of Dictionary in which a single key can have different values. It is used because some form elements have multiple values for the same key and it saves the multiple values of a key in form of a list.
Examples 1:
In this example .get() function is used to get the value from the corresponding key.
Python3
from werkzeug.datastructures import ImmutableMultiDict   data = ImmutableMultiDict([('username', 'Ryan'),                            ('password', 'QWERTY')]) print(data.get('username')) |
Output:
Ryan
Examples 2:
The same thing can be achieved even if there are many values to the same key.
Python3
from werkzeug.datastructures import ImmutableMultiDict   data = ImmutableMultiDict([('username', 'Ryan'),                            ('password', 'QWERTY'),                            ('password',123456)]) print(data.getlist('password')) |
Output:
['QWERTY', 123456]
Examples 3:
Moreover, we can also change the output result into a Dictionary type.
Python3
from werkzeug.datastructures import ImmutableMultiDict   data = ImmutableMultiDict([('username', 'Ryan'),                            ('password', 'QWERTY'),                            ('password',123456)]) print(data.to_dict(flat=False)) |
Output:
{'username': ['Ryan'], 'password': ['QWERTY', 123456]}
So, this is how you extract data from an ImmutableMultiDict
