Convert Dictionary to List in Python (3 Examples)

 

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

Here is an overview:

Let’s dive into the Python code!

 

Create Example Dictionary

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

my_dict = {"a":30,"b":25,"c":15,"d":40}
 
print(my_dict)
# {'a': 30, 'b': 25, 'c': 15, 'd': 40}
 
print(type(my_dict))
# <class 'dict'>

With the example dictionary created, we will now explore 3 examples of how to change the dictionary to a list.
 

Example 1: Turn Dictionary Keys to List Using list() Function

In this first example, we will transform the dictionary keys to a list using the list() function:

my_list = list(my_dict.keys())
 
print(my_list)
# ['a', 'b', 'c', 'd']
 
print(type(my_list))
# <class 'list'>

In the above example, we turned the dictionary keys to a list using the built-in keys() method. This method returns a view object that represents the keys of the dictionary. We then parse this view object to the list() function to create a list of the dictionary keys.
 

Example 2: Turn Dictionary Values to List Using for Loop

In this next example, we will use a for loop to change the dictionary values to a list:

my_list = []
for v in my_dict.values():
  my_list.append(v)
 
print(my_list)
# [30, 25, 15, 40]
 
print(type(my_list))
# <class 'list'>

Here, an empty list called my_list is created, then a for loop iterates through all the values in the dictionary my_dict, which have been returned using the values() method.

Each value in the dictionary is then appended to the my_list using the append() method. After all the values have been added to the my_list, which is printed using the print() function.
 

Example 3: Turn Dictionary Items to List Using List Comprehension

In this last example, we will turn the dictionary items into a list using list comprehension method:

my_list = [(k, v) for k, v in my_dict.items()]
 
print(my_list)
# [('a', 30), ('b', 25), ('c', 15), ('d', 40)]
 
print(type(my_list))
# <class 'list'>

This method creates a list of tuples that contain both keys and values of the dictionary. The list comprehension iterates over the items() of the view object and creates a tuple for each key-value pair in the dictionary.

Each tuple contains the key as the first element and the value as the second element. The resulting list contains all the tuples and represents the key-value pairs of the dictionary in a list format.

 
With that, we have demonstrated how to convert a dictionary to a list in Python. I hope you found this tutorial helpful!

 

Video, Further Resources & Summary

Do you need more explanations on converting a 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 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 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