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

Similar Reads

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

Extract multidict values to a list in Python

...

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 values of a multidict....

Extract specific values from a multidict to a list

...