Creating a multidict in Python
Here, we are creating a multidict with the key ‘b‘ having multiple values, 2 and 3, and ‘c’ with 5 and 7.
Python3
# Import the 'multidict' library import multidict # create a multidict structure by # passing the values to 'Multidict' class. d = multidict.MultiDict([( 'a' , 1 ), ( 'b' , 2 ), ( 'b' , 3 ), ( 'c' , 5 ), ( 'd' , 4 ), ( 'c' , 7 )]) print (d) |
Output:
<MultiDict(‘a’: 1, ‘b’: 2, ‘b’: 3, ‘c’: 5, ‘d’: 4, ‘c’: 7)>
Extract multidict values to a list in Python
The Multidict, is a dictionary-like structure, having key-value pairs, but the ‘same key’ can occur multiple times, in the collection. The features of a Multidict in Python are as follows:
- The insertion order of the collection is maintained.
- Multiple values in the collection can have the same key.
- The keys are stored as a ‘string’.
Installation
pip install multidict