Convert Dictionary Keys to List & Vice Versa in Python (Examples)
Hi! This tutorial will show you how to transform dictionary keys to a list and vice-versa in the Python programming language.
First, though, here is a quick overview of this tutorial:
Let’s dive right into the Python code!
Create Sample Dictionary
We will create the sample Python dictionary that will be turned into a list in this tutorial. So, in your Python IDE, run the line of code below to create the sample dictionary:
my_dict = {"Name":"Linda Fey","Age":34,"Sex":"Female","Occupation":"Hair Stylist"} print(type(my_dict)) # <class 'dict'>
As seen, the dictionary contains some personal information about someone.
Example 1: Keys to List | Turn Dictionary Keys to List Using list() Function
In this first example, we will use Python’s built-in list() constructor to turn the dictionary keys to a list.
keys_list = list(my_dict.keys()) print(keys_list) # ['Name', 'Age', 'Sex', 'Occupation'] print(type(keys_list)) # <class 'list'>
In the above example, we used the .keys()
function to extract the keys from the dictionary object and then parsed that to the list()
constructor, which turned them to a list.
Example 2: Keys to List | Turn Dictionary Keys to List Using List Comprehension
In this second example, we will use list comprehension method to transform the dictionary keys to a list.
keys_list = [keys for keys in my_dict] print(keys_list) # ['Name', 'Age', 'Sex', 'Occupation'] print(type(keys_list)) # <class 'list'>
The above code iterates through the dictionary and extracts the keys, which are then stored in the list named “keys_list” using the list comprehension above.
Example 3: Keys to List | Turn Dictionary Keys to List Using For Loop
In this third example, we will use a for loop to turn the dictionary keys to a list.
keys_list = [] for i,j in my_dict.items(): keys_list.append(i) print(keys_list) # ['Name', 'Age', 'Sex', 'Occupation'] print(type(keys_list)) # <class 'list'>
The for loop iterates through the dictionary items for the i
th key and j
th key value and then extracts the i
th key, which is then stored in the list called “keys_list” via the append() method.
Now that we have demonstrated how to extract dictionary keys and transform them into a list, we will now examine examples of how to convert the list back to the dictionary keys.
Example 1: List to Keys | Turn List to Dictionary Keys Using .fromkeys() Function
In this first example, we will use the .fromkeys()
function to turn the list back into the dictionary keys.
new_dict = dict.fromkeys(keys_list) print(new_dict) # {'Name': None, 'Age': None, 'Sex': None, 'Occupation': None} print(type(new_dict)) # <class 'dict'>
In the example above, the .fromkeys()
function converts the list into dictionary keys. But as you would notice, the dictionary returns keys with None
values. You can, however, assign values to the dictionary like so:
new_dict["Name"] = "Linda Fey" new_dict["Age"] = 34 new_dict["Sex"] = "Female" new_dict["Occupation"] = "Hair Stylist" print(new_dict) # {'Name': 'Linda Fey', 'Age': 34, 'Sex': 'Female', 'Occupation': 'Hair Stylist'}
Now we have the dictionary, new_dict, which is identical to my_dict.
Example 2: List to Keys | Turn List to Dictionary Keys Using Dictionary Comprehension
In this second example, we will use dictionary comprehension to turn the list back into dictionary keys.
new_dict = {i: None for i in keys_list} print(new_dict) # {'Name': None, 'Age': None, 'Sex': None, 'Occupation': None} print(type(new_dict)) # <class 'dict'>
Just as in the previous example, the dictionary returns keys with None
values. We can assign values again.
new_dict['Name'] = "Linda Fey" new_dict["Age"] = 34 new_dict["Sex"] = "Female" new_dict["Occupation"] = "Hair Stylist" print(new_dict) # {'Name': 'Linda Fey', 'Age': 34, 'Sex': 'Female', 'Occupation': 'Hair Stylist'}
With that, we have demonstrated how to convert dictionary keys to a list and vice-versa in Python. I hope you found this tutorial helpful!
Video, Further Resources & Summary
Do you need more explanations on how to convert dictionary keys to a list and vice-versa 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 dictionary keys to a list and vice-versa 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:
- Convert Python List to PyTorch Tensor & Vice-Versa in Python (Examples)
- Access Elements in List within Dictionary in Python (2 Examples)
- Learn Python Programming
- Convert List from Boolean to Float in Python (2 Examples)
- Convert List of Tuples to List of Lists in Python (3 Examples)
This post has shown how to convert dictionary keys to a list and vice-versa in Python. In case you have further questions, you may leave a comment below.
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.
Statistics Globe Newsletter