Convert Nested Dictionary to List in Python (2 Examples)

 

Hi! This short tutorial will show you how to turn a nested dictionary to a list in the Python programming language.

Here is an overview:

Let’s jump into the Python code!

 

Create Example Nested Dictionary

Here, we will create the example nested Python dictionary that will be transformed to a Python list in this tutorial. Therefore, in your preferred Python IDE, run the code below to create the example nested dictionary:

nested_dict = {"A": {"X": 10, "Y": 20}, "B": {"X": 30, "Y": 40}}
 
print(nested_dict)
# {'A': {'X': 10, 'Y': 20}, 'B': {'X': 30, 'Y': 40}}
 
print(type(nested_dict))
# <class 'dict'>

With the example nested dictionary created, we will now examine 2 ways to change the nested dictionary to a list.
 

Example 1: Transform Nested Dictionary to List Using Nested for Loop

In this first example, we are going to use a nested for loop to change the nested dictionary to a list:

my_list = []
for k,v in nested_dict.items():
  for i,j in v.items():
    my_list.append((k,i,j))
 
print(my_list)
# [('A', 'X', 10), ('A', 'Y', 20), ('B', 'X', 30), ('B', 'Y', 40)]
 
print(type(my_list))
# <class 'list'>

This method uses a nested dictionary nested_dict, which has two top-level keys, “A” and “B”, each of which is associated with another dictionary. These nested dictionaries have two keys, “X” and “Y”, which are associated with some integer values.

An empty list called my_list is created, and then a nested loop is used to iterate over the items of the dictionary. The outer loop iterates over the keys and values of the nested_dict, while the inner loop iterates over the keys and values of each nested dictionary.

For each iteration of the inner loop, a tuple is appended to my_list containing the outer key k, the inner key i, and the inner value j. The tuple is constructed using parentheses with comma-separated values inside them. The append() method is used to add the tuple to the list.

Finally, my_list, which contains all the items of the nested dictionary as tuples, is printed using the print() function.

 

Example 2: Transform Nested Dictionary to List Using List Comprehension

In this next example, we will use list comprehension to turn the nested dictionary to a list:

my_list = [(k,i,j) for k,v in nested_dict.items() for i,j in v.items()]
 
print(my_list)
# [('A', 'X', 10), ('A', 'Y', 20), ('B', 'X', 30), ('B', 'Y', 40)]
 
print(type(my_list))
# <class 'list'>

The above example uses a list comprehension to create a list of tuples, where each tuple contains the key-value pairs of the nested dictionary flattened into a single level. The list comprehension uses a nested loop to iterate over the items of the dictionary.

The outer loop for k,v in nested_dict.items() iterates over the top-level key-value pairs of the nested_dict, which utilizes the items() method to get the keys and values in the dictionary. For each key-value pair, the inner loop for i,j in v.items() iterates over the key-value pairs of the nested dictionary associated with that key.

For each iteration of the inner loop, the list comprehension creates a new tuple containing the three items k, i, and j, which are the top-level key, the nested dictionary key, and the nested dictionary value, respectively.

The list comprehension encloses this tuple in square brackets to create a list of tuples and returns the list as the output of the comprehension. This list of tuples is assigned to the variable my_list.

Finally, my_list, which contains all the items of the nested dictionary as tuples, is printed using the print() function.

 
So, with the two examples above, we have demonstrated how to convert a nested dictionary to a list in Python. I hope you found this tutorial helpful!
 

Video, Further Resources & Summary

Do you need more explanations on how to convert a nested dictionary to a list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to convert a nested dictionary to a list in Python.

 

The YouTube video will be added soon.

 

Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:

This post has shown how to convert a nested dictionary to a list in Python. In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top