Extract multidict values to a list in Python
Last Updated :
26 Apr, 2025
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 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
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 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 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 keys
list_for_key_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 keys in separate list
list_for_key_of_multidict.append(k)
# print the lists
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 the Multidict library
import multidict
# Declare a Multidict structure
# using the Multidict class
d = multidict.MultiDict([('a', 1), ('b', 2),
('b', 3), ('c', 5),
('d', 4), ('c', 7)])
# Fetch values for key 'b' in a
# list using getall(k) method
values_for_keyB_list = d.getall('b')
# Print the list of values
print("Key B values:", values_for_keyB_list)
# Fetch values for key 'c' in a
# list using getall(k) method
values_for_keyC_list = d.getall('c')
# Print the list of values
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 the package Multidict
import multidict
# Create multidict structure using
# the Multidict class
d = multidict.MultiDict([('a', 1), ('b', 2),
('b', 3), ('c', 5),
('d', 4), ('c', 7)])
# Use the popall(k) method to
# fetch a list of all
# values associated with key 'c'
pop_list_of_key_c = d.popall('c')
# print the popped values
print("Pop out values of key C:", pop_list_of_key_c)
# After popping the key values,
# print the Multidict structure
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)>
Similar Reads
Convert Tuple to List in Python
In Python, tuples and lists are commonly used data structures, but they have different properties:Tuples are immutable: their elements cannot be changed after creation.Lists are mutable: they support adding, removing, or changing elements.Sometimes, you may need to convert a tuple to a list for furt
2 min read
How to convert a MultiDict to nested dictionary using Python
A MultiDict is a dictionary-like object that holds multiple values for the same key, making it a useful data structure for processing forms and query strings. It is a subclass of the Python built-in dictionary and behaves similarly. In some use cases, we may need to convert a MultiDict to a nested d
3 min read
Ways to create a dictionary of Lists - Python
A dictionary of lists is a type of dictionary where each value is a list. These dictionaries are commonly used when we need to associate multiple values with a single key.Initialize a Dictionary of ListsThis method involves manually defining a dictionary where each key is explicitly assigned a list
3 min read
Convert List Of Tuples To Json Python
Working with data often involves converting between different formats, and JSON is a popular choice for data interchange due to its simplicity and readability. In Python, converting a list of tuples to JSON can be achieved through various approaches. In this article, we'll explore four different met
3 min read
Filter Python list by Predicate in Python
A predicate is a function that returns either True or False for a given input. By applying this predicate to each element of a list, we can create a new list containing only the elements that satisfy the condition. Let's explore different methods to filter a list based on a predicate.Using list comp
3 min read
How to extract the data from an ImmutableMultiDict
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 multi
1 min read
Convert a list of Tuples into Dictionary - Python
Converting a list of tuples into a dictionary involves transforming each tuple, where the first element serves as the key and the second as the corresponding value. For example, given a list of tuples a = [("a", 1), ("b", 2), ("c", 3)], we need to convert it into a dictionary. Since each key-value p
3 min read
Convert string to a list in Python
Our task is to Convert string to a list in Python. Whether we need to break a string into characters or words, there are multiple efficient methods to achieve this. In this article, we'll explore these conversion techniques with simple examples. The most common way to convert a string into a list is
2 min read
Converting MultiDict to proper JSON
In this article, we will see what is MultiDict, and how do we convert a multidict into JSON files in Python. First, we will convert a multidict to a dictionary data type, and at last, we dump that dictionary into a JSON file. Functions Used :Â json.dump(): JSON module in Python module provides a meth
2 min read
Read a CSV into list of lists in Python
In this article, we are going to see how to read CSV files into a list of lists in Python. Method 1: Using CSV moduleWe can read the CSV files into different data structures like a list, a list of tuples, or a list of dictionaries.We can use other modules like pandas which are mostly used in ML appl
2 min read