Sometimes, while working with a Python dictionary, we can have a problem in which we need to perform the flattening of a key-value pair of dictionaries to a list and convert it to a list of lists. This can have applications in domains in which we have data. Let’s discuss certain ways in which this task can be performed.
Method #1: Using loop + items() This brute force way in which we can perform this task. In this, we loop through all the pairs and extract list value elements using items() and render them in a new list.
Python3
# Python3 code to demonstrate working of # Convert Key-Value list Dictionary to Lists of List # Using loop + items() # initializing Dictionary test_dict = { 'gfg' : [ 1 , 3 , 4 ], 'is' : [ 7 , 6 ], 'best' : [ 4 , 5 ]} # printing original dictionary print ( "The original dictionary is : " + str (test_dict)) # Convert Key-Value list Dictionary to Lists of List # Using loop + items() res = [] for key, val in test_dict.items(): res.append([key] + val) # printing result print ( "The converted list is : " + str (res)) |
The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]]
Time complexity: O(n), where n is the number of items in the dictionary
Auxiliary space: O(n), as a new list “res” with n items is created.
Method #2: Using list comprehension This task can also be performed using list comprehension. In this, we perform the task similar to the above method just in a one-liner shorter way.
Python3
# Python3 code to demonstrate working of # Convert Key-Value list Dictionary to Lists of List # Using list comprehension # initializing Dictionary test_dict = { 'gfg' : [ 1 , 3 , 4 ], 'is' : [ 7 , 6 ], 'best' : [ 4 , 5 ]} # printing original dictionary print ( "The original dictionary is : " + str (test_dict)) # Convert Key-Value list Dictionary to Lists of List # Using list comprehension res = [[key] + val for key, val in test_dict.items()] # printing result print ( "The converted list is : " + str (res)) |
The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]]
Time complexity: O(n), where n is the number of key-value pairs in the dictionary
Auxiliary space: O(n), where n is the number of key-value pairs in the dictionary.
Method #3: Using map and dict.keys() This task can also be performed. In this, we get the keys value of dict and iterate over the list of keys and get the values of corresponding values and concatenate both key and value, and form a list of lists.
Python
# Python3 code to demonstrate working of # Convert Key-Value list Dictionary to Lists of List # Using map + keys() # initializing Dictionary test_dict = { 'gfg' : [ 1 , 3 , 4 ], 'is' : [ 7 , 6 ], 'best' : [ 4 , 5 ]} # printing original dictionary print ( "The original dictionary is : " + str (test_dict)) temp1 = list (test_dict.keys()) # Convert Key-Value list Dictionary to Lists of List # Using map + keys() res = list ( map ( lambda i: [i] + test_dict[i], temp1)) # printing result print ( "The converted list is : " + str (res)) |
The original dictionary is : {'is': [7, 6], 'gfg': [1, 3, 4], 'best': [4, 5]} The converted list is : [['is', 7, 6], ['gfg', 1, 3, 4], ['best', 4, 5]]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4 : Using keys() and insert() method
Python3
# Python3 code to demonstrate working of # Convert Key-Value list Dictionary to Lists of List # initializing Dictionary test_dict = { 'gfg' : [ 1 , 3 , 4 ], 'is' : [ 7 , 6 ], 'best' : [ 4 , 5 ]} # printing original dictionary print ( "The original dictionary is : " + str (test_dict)) # Convert Key-Value list Dictionary to Lists of List res = [] for i in test_dict.keys(): test_dict[i].insert( 0 , i) res.append(test_dict[i]) # printing result print ( "The converted list is : " + str (res)) |
The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]]
Time complexity: O(n), where n is the number of keys in the dictionary. The for loop runs n times and the operations inside the loop take constant time.
Auxiliary Space: O(n), where n is the number of keys in the dictionary. The space required to store the output list ‘res’ is proportional to the number of keys in the dictionary. The size of the input dictionary is not taken into account as it is not relevant to the space required for the output list.
Method #5: Using zip() function and list comprehension
In this approach, we can use the zip() function to combine the keys and values of the dictionary. We then use a list comprehension to convert each combined key-value pair into a list and store them in a final list.
Python3
# Python3 code to demonstrate working of # Convert Key-Value list Dictionary to Lists of List # Using zip() + list comprehension # initializing Dictionary test_dict = { 'gfg' : [ 1 , 3 , 4 ], 'is' : [ 7 , 6 ], 'best' : [ 4 , 5 ]} # printing original dictionary print ( "The original dictionary is : " + str (test_dict)) # Convert Key-Value list Dictionary to Lists of List # Using zip() + list comprehension res = [ [key] + value for key, value in zip (test_dict.keys(), test_dict.values()) ] # printing result print ( "The converted list is : " + str (res)) |
The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]]
Time complexity: O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(n), where n is the number of key-value pairs in the dictionary, to store the final list.
Method #6: Using a nested list comprehension
Use a nested list comprehension to iterate over the keys in the dictionary and append the corresponding key and value as a list to the result list.
Step-by-step approach:
- Initialize a dictionary with key-value pairs, where each value is a list.
- Create an empty list to store the converted list of lists.
- Iterate over the key-value pairs in the dictionary using a loop, or a list comprehension.
- For each key-value pair, create a new list that consists of the key followed by the list of values.
- Append the new list to the list of lists.
- Return the list of lists.
Below is the implementation of the above approach:
Python
# initializing Dictionary test_dict = { 'gfg' : [ 1 , 3 , 4 ], 'is' : [ 7 , 6 ], 'best' : [ 4 , 5 ]} # printing original dictionary print ( "The original dictionary is : " + str (test_dict)) # Convert Key-Value list Dictionary to Lists of List # Using a nested list comprehension res = [[key] + test_dict[key] for key in test_dict] # printing result print ( "The converted list is : " + str (res)) |
The original dictionary is : {'is': [7, 6], 'gfg': [1, 3, 4], 'best': [4, 5]} The converted list is : [['is', 7, 6], ['gfg', 1, 3, 4], ['best', 4, 5]]
Time complexity: O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(n), since we are creating a new list to store the result.
Method 7: Using the items() method of the dictionary along with a for loop.
Step-by-step approach:
- Initialize an empty list res.
- Iterate over the items in the test_dict dictionary using a for loop.
- For each item, create a list that contains the key of the item followed by the value of the item.
- Append the list created in step 3 to the res list.
- Print the res list.
Below is the implementation of the above approach:
Python3
# initializing Dictionary test_dict = { 'gfg' : [ 1 , 3 , 4 ], 'is' : [ 7 , 6 ], 'best' : [ 4 , 5 ]} # printing original dictionary print ( "The original dictionary is : " + str (test_dict)) # Convert Key-Value list Dictionary to Lists of List # Using items() method and for loop res = [] for key, value in test_dict.items(): res.append([key] + value) # printing result print ( "The converted list is : " + str (res)) |
The original dictionary is : {'gfg': [1, 3, 4], 'is': [7, 6], 'best': [4, 5]} The converted list is : [['gfg', 1, 3, 4], ['is', 7, 6], ['best', 4, 5]]
Time complexity: O(n), where n is the number of key-value pairs in the test_dict dictionary.
Auxiliary space: O(n), where n is the number of key-value pairs in the test_dict dictionary (to store the res list).