Convert Dictionary of Dictionaries to Python List of Dictionaries
Last Updated :
23 Jan, 2025
We are given dictionary of dictionaries we need to convert it to list of dictionaries. For example, For example, we are having d = { 'A': {'color': 'red', 'shape': 'circle'}, 'B': {'color': 'blue', 'shape': 'square'}, 'C': {'color': 'green', 'shape': 'triangle'} } we need to convert it to a list of dictionaries so that the output becomes [{'color': 'red', 'shape': 'circle'}, {'color': 'blue', 'shape': 'square'}, {'color': 'green', 'shape': 'triangle'}].
Using List Comprehension
list comprehension iterates over the values of the dictionary which are the inner dictionaries and constructs a list of these inner dictionaries. This allows for a concise conversion of a dictionary of dictionaries into a list of dictionaries.
Python
d = {
'A': {'color': 'red', 'shape': 'circle'},
'B': {'color': 'blue', 'shape': 'square'},
'C': {'color': 'green', 'shape': 'triangle'}
}
# Convert dictionary of dictionaries to a list of dictionaries using list comprehension
res = [val for val in d.values()]
print(res)
Output[{'color': 'red', 'shape': 'circle'}, {'color': 'blue', 'shape': 'square'}, {'color': 'green', 'shape': 'triangle'}]
Explanation:
d
retrieves the values (which are dictionaries) of the outer dictionary.- List comprehension collects these dictionaries into a list.
Using map()
map()
function applies a lambda
function to each value in the dictionary, where each value is an inner dictionary. lambda x: x
returns the inner dictionaries and list()
converts the result from map()
into a list of dictionaries.
Python
d = {
'A': {'color': 'red', 'shape': 'circle'},
'B': {'color': 'blue', 'shape': 'square'},
'C': {'color': 'green', 'shape': 'triangle'}
}
# Convert dictionary of dictionaries to a list of dictionaries
res = list(map(lambda x: x[1], d.items()))
print(res)
Output[{'color': 'red', 'shape': 'circle'}, {'color': 'blue', 'shape': 'square'}, {'color': 'green', 'shape': 'triangle'}]
Explanation:
d
returns each key-value pair from the outer dictionary.map()
is used to extract only the values (which are the inner dictionaries) and convert them into a list.
Using for
Loop
for
loop iterates over the values of the dictionary (which are the inner dictionaries) and creates a list containing those inner dictionaries, resulting in a list of dictionaries
Python
d = {
'A': {'color': 'red', 'shape': 'circle'},
'B': {'color': 'blue', 'shape': 'square'},
'C': {'color': 'green', 'shape': 'triangle'}
}
# Convert dictionary of dictionaries to a list of dictionaries
res = []
for key, value in d.items():
res.append(value)
print(res)
Output[{'color': 'red', 'shape': 'circle'}, {'color': 'blue', 'shape': 'square'}, {'color': 'green', 'shape': 'triangle'}]
Explanation:
for
loop iterates over the items in d
, appending each inner dictionary to the result
list.
Similar Reads
Convert List of Dictionaries to Dictionary of Lists - Python We are given a list of dictionaries we need to convert it to dictionaries of lists. For example, we are given a list of dictionaries li = [{'manoj': 'java', 'bobby': 'python'}, {'manoj': 'php', 'bobby': 'java'}, {'manoj': 'cloud', 'bobby': 'big-data'}] we need to convert this to dictionary of list s
3 min read
Convert a List of Dictionaries into a Set of Dictionaries Python's versatility allows developers to manipulate data in various ways. When working with a list of dictionaries, there might be scenarios where you want to convert it into a set of dictionaries to eliminate duplicates or for other reasons. In this article, we'll explore three different methods t
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
Python - Convert Dictionaries List to Order Key Nested dictionaries Given list of dictionaries, our task is to convert a list of dictionaries into a dictionary where each key corresponds to the index of the dictionary in the list and the value is the dictionary itself. For example: consider this list of dictionaries: li = [{"Gfg": 3, 4: 9}, {"is": 8, "Good": 2}] the
3 min read
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
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