Python – Convert Key-Value list Dictionary to List of Lists
Sometimes, while working with Python dictionary, we can have a problem in which we need to perform the flattening a key value pair of dictionary to a list and convert to lists of list. This can have applications in domains in which we have data. Lets 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 in a new list.
# 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]]
Method #2 : Using list comprehension
This task can also be performed using list comprehension. In this, we perform the task similar to above method just in one-liner shorter way.
# 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]]
Recommended Posts:
- Python - Convert Lists of List to Dictionary
- Python | Convert list of tuples to dictionary value lists
- Python | Convert List of Dictionary to Tuple list
- Python | Convert List of lists to list of Strings
- Python - Convert List of Dictionaries to List of Lists
- Python | Convert list into list of lists
- Python | Split dictionary of lists to list of dictionaries
- Python | Convert a list to dictionary
- Python - Convert Frequency dictionary to list
- Python | Convert list of tuple into dictionary
- Python | Convert dictionary to list of tuples
- Python | Convert a list of Tuples into Dictionary
- Python | Convert a list into tuple of lists
- Python - Convert Records List to Segregated Dictionary
- Python - Convert key-values list to flat dictionary
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

