Extract multidict values to a list in Python
Last Updated :
07 Nov, 2022
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
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 multidict
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
Here, we are Creating an empty list, and then using a Python for loop we append only the values of a multidict.
Python3
import multidict
d = multidict.MultiDict([( 'a' , 1 ), ( 'b' , 2 ),
( 'b' , 3 ), ( 'c' , 5 ),
( 'd' , 4 ), ( 'c' , 7 )])
list_for_values_of_multidict = []
for k, v in d.items():
list_for_values_of_multidict.append(v)
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 keys to a list in Python
Here, we are Creating an empty list, and then using a Python for loop we append only the key of a multidict.
Python3
import multidict
d = multidict.MultiDict([( 'a' , 1 ), ( 'b' , 2 ),
( 'b' , 3 ), ( 'c' , 5 ),
( 'd' , 4 ), ( 'c' , 7 )])
list_for_key_of_multidict = []
for k, v in d.items():
list_for_key_of_multidict.append(k)
print ( "List of keys of multidict:" , list_for_key_of_multidict)
|
Output:
List of keys of multidict: [‘a’, ‘b’, ‘b’, ‘c’, ‘d’, ‘c’]
Extract specific values from a multidict to a list
Example 1: Using getall()
This method returns the values associated with a particular key of Multidict. We need to pass the key whose values are desired as a parameter to the method. The method returns a list of values for the key. It will throw a ‘KeyError’ if the specified key is not found.
Python3
import multidict
d = multidict.MultiDict([( 'a' , 1 ), ( 'b' , 2 ),
( 'b' , 3 ), ( 'c' , 5 ),
( 'd' , 4 ), ( 'c' , 7 )])
values_for_keyB_list = d.getall( 'b' )
print ( "Key B values:" , values_for_keyB_list)
values_for_keyC_list = d.getall( 'c' )
print ( "Key C values:" , values_for_keyC_list)
|
Output:
Key B values: [2, 3]
Key C values: [5, 7]
Example 2: Using popall()
This method returns the values associated with a particular key of Multidict. If the key specified, is in a Multidict structure then it removes all occurrences of the key and returns a list of values of the key. It throws a ‘KeyError’ if the specified key is not found.
Python3
import multidict
d = multidict.MultiDict([( 'a' , 1 ), ( 'b' , 2 ),
( 'b' , 3 ), ( 'c' , 5 ),
( 'd' , 4 ), ( 'c' , 7 )])
pop_list_of_key_c = d.popall( 'c' )
print ( "Pop out values of key C:" , pop_list_of_key_c)
print ( "Multidict after popping key C:" , d)
|
Output:
Pop out values of key C: [5, 7]
Multidict after popping key C: <MultiDict(‘a’: 1, ‘b’: 2, ‘b’: 3, ‘d’: 4)>
Share your thoughts in the comments
Please Login to comment...