Extract multidict values to a list in Python
Here, we are Creating an empty list, and then using a Python for loop we append only the values of a multidict.
Python3
# Import module 'Multidict' import multidict # create a multidict d = multidict.MultiDict([( 'a' , 1 ), ( 'b' , 2 ), ( 'b' , 3 ), ( 'c' , 5 ), ( 'd' , 4 ), ( 'c' , 7 )]) # create two blank lists to store # the values list_for_values_of_multidict = [] # Loop through the multidict structure # using "items" method Use append method # of list to add respective keys and # values of multidict for k, v in d.items(): # place the values in separate list list_for_values_of_multidict.append(v) # print the lists print ( "List of values of multidict:" , list_for_values_of_multidict) |
Output:
List of values of multidict: [1, 2, 3, 5, 4, 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