Open In App

Extract multidict values to a list in Python

Last Updated : 07 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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)>



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads