Python - Convert Key-Value list Dictionary to List of Lists
Last Updated :
21 Jan, 2025
We are given a key value list dictionary we need to convert it list of lists. For example we are given a dictionary a = {'name': 'Geeks', 'age': 8, 'city': 'Noida'} we need to convert this into list of lists so the output should be [['name', 'Geeks'], ['age', 25], ['city', 'Geeks']].
Using List Comprehension
List comprehension iterates over each key-value pair in the dictionary, extracting both the key and its corresponding value. These pairs are then collected into sublists, resulting in a list of lists.
Python
a = {'name': 'Alice', 'age': 25, 'city': 'New York'} # Dictionary of key-value pairs
# Use list comprehension to iterate through the dictionary items, creating a list of lists with [key, value] pairs
res = [[key, value] for key, value in a.items()]
print(res)
Output[['name', 'Alice'], ['age', 25], ['city', 'New York']]
Explanation:
- List comprehension iterates over the key-value pairs in the dictionary a using .items().
- For each key-value pair, a sublist [key, value] is created, resulting in a list of lists.
Using map()
map()
function applies a lambda to each key-value pair in the dictionary, converting each pair into a list. The result is then converted to a list, creating a list of lists with key-value pairs.
Python
# Define a dictionary with key-value pairs
a = {'name': 'Alice', 'age': 25, 'city': 'New York'}
# Use map to iterate over each item (key-value pair) in the dictionary
res = list(map(lambda item: [item[0], item[1]], a.items()))
print(res)
Output[['name', 'Alice'], ['age', 25], ['city', 'New York']]
Explanation:
- map() function iterates over each key-value pair in the dictionary a, transforming each pair into a list containing the key and the corresponding value ([item[0], item[1]]).
- list() function converts the result from map() (an iterable) into a list of lists, which is then printed as the final output
Using dict.items()
and list()
dict.items() method provides the dictionary's key-value pairs as tuples. Using list(), these pairs are converted into a list of lists, where each list contains a key-value pair.
Python
# Define a dictionary with key-value pairs
a = {'name': 'Alice', 'age': 25, 'city': 'New York'}
# which converts each tuple into a list of the form [key, value]
res = list(map(list, a.items()))
print(res)
Output[['name', 'Alice'], ['age', 25], ['city', 'New York']]
Explanation:
map(list, a.items())
applies the list()
function to each key-value pair (tuple) from the dictionary a
, converting each tuple into a list of the form [key, value]
.list()
function collects the results from map()
into a list of lists, which is then printed as the final output.
Similar Reads
Python - Convert List of Dictionaries to List of Lists We are given list of dictionaries we need to convert it to list of lists. For example we are given a list of dictionaries a = [{'name': 'Geeks', 'age': 25}, {'name': 'Geeks', 'age': 30}] we need to convert it in list of list so that the output becomes[['Geeks',25],['Geeks;'30]].Using List Comprehens
3 min read
Convert List of Tuples to Dictionary Value Lists - Python The task is to convert a list of tuples into a dictionary where the first element of each tuple serves as the key and the second element becomes the value. If a key appears multiple times in the list, its values should be grouped together in a list.For example, given the list li = [(1, 'gfg'), (1, '
4 min read
Convert List to Single Dictionary Key - Value list - Python We are given a list and a element K, our aim is to transform the given list into a dictionary where the specified element (Kth element) becomes the key and the rest of the elements form the value list. For example: if the given list is: [6, 5, 3, 2] and K = 1 then the output will be {5: [6, 3, 2]}.U
4 min read
Convert List of Lists to Dictionary - Python We are given list of lists we need to convert it to python . For example we are given a list of lists a = [["a", 1], ["b", 2], ["c", 3]] we need to convert the list in dictionary so that the output becomes {'a': 1, 'b': 2, 'c': 3}. Using Dictionary ComprehensionUsing dictionary comprehension, we ite
3 min read
Python - Convert list of dictionaries to Dictionary Value list We are given a list of dictionaries we need to convert it to dictionary. For example, given a list of dictionaries: d = [{'a': 1, 'b': 2}, {'a': 3, 'b': 4}, {'a': 5, 'b': 6}], the output should be: {'a': [1, 3, 5], 'b': [2, 4, 6]}.Using Dictionary ComprehensionUsing dictionary comprehension, we can
3 min read