Python - Convert Key-Value list Dictionary to List of Lists
Last Updated :
12 Jul, 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.
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice