Open In App

Python | Split dictionary of lists to list of dictionaries

Improve
Improve
Like Article
Like
Save
Share
Report

Conversion from one data type to other is essential in various facets of programming. Be it development or competitive programming. Hence knowledge of it is quite useful and necessary. Let’s discuss certain methods by which dictionary of list can be converted to the corresponding list of dictionaries. 

Method #1 : Using list comprehension We can use list comprehension as the one-liner alternative to perform various naive tasks providing readability with a more concise code. We can iterate through each of dictionary element and corresponding keep constructing the list of dictionary. 

Python3




# Python3 code to demonstrate
# to convert dictionary of list to
# list of dictionaries
# using list comprehension
 
# initializing dictionary
test_dict = { "Rash" : [1, 3], "Manjeet" : [1, 4], "Akash" : [3, 4] }
 
# printing original dictionary
print ("The original dictionary is : " + str(test_dict))
 
# using list comprehension
# to convert dictionary of list to
# list of dictionaries
res = [{key : value[i] for key, value in test_dict.items()}
         for i in range(2)]
 
# printing result
print ("The converted list of dictionaries " +  str(res))


Output:

The original dictionary is : {‘Rash’: [1, 3], ‘Manjeet’: [1, 4], ‘Akash’: [3, 4]} The converted list of dictionaries [{‘Rash’: 1, ‘Manjeet’: 1, ‘Akash’: 3}, {‘Rash’: 3, ‘Manjeet’: 4, ‘Akash’: 4}]

Time Complexity: O(n2)

Space Complexity: O(n)

  Method #2 : Using zip() This approach used zip function two times, first time when we need to zip the particular index value of all lists as one and second to get all values of particular index and zip it with the corresponding keys. 

Python3




# Python3 code to demonstrate
# to convert dictionary of list to
# list of dictionaries
# using zip()
 
# initializing dictionary
test_dict = { "Rash" : [1, 3], "Manjeet" : [1, 4], "Akash" : [3, 4] }
 
# printing original dictionary
print ("The original dictionary is : " + str(test_dict))
 
# using zip()
# to convert dictionary of list to
# list of dictionaries
res = [dict(zip(test_dict, i)) for i in zip(*test_dict.values())]
 
# printing result
print ("The converted list of dictionaries " +  str(res))


Output:

The original dictionary is : {‘Rash’: [1, 3], ‘Akash’: [3, 4], ‘Manjeet’: [1, 4]} The converted list of dictionaries [{‘Rash’: 1, ‘Akash’: 3, ‘Manjeet’: 1}, {‘Rash’: 3, ‘Akash’: 4, ‘Manjeet’: 4}]

Time Complexity: O(n)
Auxiliary Space: O(n)

Method 3 : Using a loop to iterate through the dictionary and create a new dictionary for each key-value pair in the original dictionary.

Step-by-step approach ;

  1. The program initializes a dictionary called test_dict with key-value pairs where the key is a string and the value is a list of integers.
  2. The program prints the original dictionary using the print() function and string concatenation.
  3. The program initializes an empty list called res which will hold the converted list of dictionaries.
  4. The program uses a loop to iterate through the key-value pairs in the test_dict.
  5. Inside the loop, the program uses another loop to iterate through the values in the list.
  6. The program checks if the length of the res list is less than or equal to the current index i. If it is, it means there are no dictionaries in res yet that can hold the current key-value pair.
  7. If step 6 is true, the program appends an empty dictionary to res.
  8. The program adds a new key-value pair to the ith dictionary in res where the key is the current key from test_dict and the value is the current value from the list.
  9. The program repeats steps 5-8 for all values in all lists in test_dict.
  10. The program prints the converted list of dictionaries using the print() function and string concatenation.
     

Python3




# Python3 code to demonstrate
# to convert dictionary of list to
# list of dictionaries
# using loop
 
# initializing dictionary
test_dict = {"Rash": [1, 3], "Manjeet": [1, 4], "Akash": [3, 4]}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# using loop
# to convert dictionary of list to
# list of dictionaries
res = []
for key, val in test_dict.items():
    for i, v in enumerate(val):
        if len(res) <= i:
            res.append({})
        res[i][key] = v
 
# printing result
print("The converted list of dictionaries " + str(res))


Output

The original dictionary is : {'Rash': [1, 3], 'Manjeet': [1, 4], 'Akash': [3, 4]}
The converted list of dictionaries [{'Rash': 1, 'Manjeet': 1, 'Akash': 3}, {'Rash': 3, 'Manjeet': 4, 'Akash': 4}]

Time complexity: O(n*m), where n is the number of keys in the dictionary and m is the length of the longest list value.
Auxiliary space: O(m), where m is the length of the longest list value, to store the list of dictionaries.



Last Updated : 13 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads