![]() |
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:
Installation pip install multidict Creating a multidict in PythonHere, we are creating a multidict with the key ‘b‘ having multiple values, 2 and 3, and ‘c’ with 5 and 7. Python3
Output:
Extract multidict values to a list in PythonHere, we are Creating an empty list, and then using a Python for loop we append only the values of a multidict. Python3
Output:
Extract multidict keys to a list in PythonHere, we are Creating an empty list, and then using a Python for loop we append only the key of a multidict. Python3
Output:
Extract specific values from a multidict to a listExample 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
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
Output:
|
Reffered: https://www.geeksforgeeks.org
Python |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 9 |