Open In App

Convert Nested Dictionary to List Python

Last Updated : 19 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Python dictionaries are versatile data structures that allow the storage of key-value pairs. However, working with nested dictionaries, where values themselves are dictionaries, can sometimes be challenging. In this article, we will explore various simple and commonly used methods to convert a nested dictionary into a list in Python.

Example:

Input: {"a":{"b":"c"}}
Output: ["a","b","c"]

Convert Nested Dictionary to List Python

Below, are the methods to Convert Nested Dictionary to List Python.

Convert Nested Dictionary to List Python Using Nested Loop

In this example, below code initializes a nested dictionary and converts its values into a flat list using a nested loop, with each element representing a key, an inner key, and its corresponding value. The resulting list `l` is printed along with the types of the original dictionary and the generated list.

Python




# Initiating dictionary
dictionary = {"a":{"b":"c"}}
 
l = []
 
# converting dictionary values to list
for k,v in dictionary.items():
  for i,j in v.items():
    l.extend([k, i, j])
 
print(type(dictionary))
print(l)
print(type(l))


Output

<type 'dict'>
['a', 'b', 'c']
<type 'list'>

Convert Nested Dictionary to List Python Using List Comprehension

In this example, below code initializes a nested dictionary and converts its values into a flat list using a list comprehension. Each element in the resulting list `l` represents a key, an inner key, and its corresponding value. The types of the original dictionary and the generated list are then printed.

Python




# Initiating dictionary
dictionary = {"a":{"b":"c"}, "d":{"e":"f"}}
 
# converting dictionary values to list
l = [p for k,v in dictionary.items() for i,j in v.items() for p in (k, i, j)]
     
print(type(dictionary))
print(l)
print(type(l))


Output

<type 'dict'>
['a', 'b', 'c', 'd', 'e', 'f']
<type 'list'>

Convert Nested Dictionary to List Python Using Recursive Function

In this example, code defines a `flatten_dict` function that recursively flattens a nested dictionary into a list, preserving the order of keys and values. The `nested_dict` is an example input, and the resulting `flattened_list` contains the flattened elements. The code then prints the types of the original nested dictionary.

Python3




def flatten_dict(d):
    flattened_list = []
    for key, value in d.items():
        flattened_list.append(key)
        if isinstance(value, dict):
            flattened_list.extend(flatten_dict(value))
        else:
            flattened_list.append(value)
    return flattened_list
 
nested_dict = {"a": {"b": "c"}, "d": {"e": "f"}}
flattened_list = flatten_dict(nested_dict)
print(type(nested_dict))
print(flattened_list)
print(type(flattened_list))


Output

<class 'dict'>
['a', 'b', 'c', 'd', 'e', 'f']
<class 'list'>

Conclusion

Converting a nested dictionary into a list in Python can be accomplished through various methods, each offering its own advantages. Whether you prefer the simplicity of list comprehension, the flexibility of a recursive function, or the functional programming style using itertools.chain, these techniques allow you to handle nested data structures effectively in your Python projects.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads