How to Convert a Python Dictionary Into a List
Python is a versatile programming language that offers numerous functionalities to developers. One such functionality is to convert a Python dictionary into a list. A dictionary is an unordered collection of key-value pairs, whereas a list is an ordered collection of elements. There may be situations when you might need to manipulate a dictionary into a list. In this article, we will explore how you can easily convert a Python dictionary into a list.
Converting a Python Dictionary to a List Using Keys()
The first method to convert a Python dictionary into a list is by using the keys() method. The keys() method returns a list of all the keys present in the dictionary. Here’s how you can do it:
“`
my_dict = {‘name’: ‘John’, ‘age’: 26, ‘occupation’: ‘Data Scientist’}
my_list = list(my_dict.keys())
print(my_list)
“`
Output: [‘name’, ‘age’, ‘occupation’]
In the above code, we have created a dictionary called `my_dict` with three key-value pairs. We then use the keys() method to extract all the keys from the dictionary and convert it into a list using the list() constructor. Finally, we print the list.
Converting a Python Dictionary to a List Using Values()
Another method to convert a Python dictionary into a list is by using the values() method. The values() method returns a list of all the values present in the dictionary. Here’s how you can do it:
“`
my_dict = {‘name’: ‘John’, ‘age’: 26, ‘occupation’: ‘Data Scientist’}
my_list = list(my_dict.values())
print(my_list)
“`
Output: [‘John’, 26, ‘Data Scientist’]
In the above code, we have created a dictionary called `my_dict` with three key-value pairs. We then use the values() method to extract all the values from the dictionary and convert it into a list using the list() constructor. Finally, we print the list.
Converting a Python Dictionary to a List Using Items()
The third method to convert a Python dictionary into a list is by using the items() method. The items() method returns a list of tuples, where each tuple contains a key-value pair. Here’s how you can do it:
“`
my_dict = {‘name’: ‘John’, ‘age’: 26, ‘occupation’: ‘Data Scientist’}
my_list = list(my_dict.items())
print(my_list)
“`
Output: [(‘name’, ‘John’), (‘age’, 26), (‘occupation’, ‘Data Scientist’)]
In the above code, we have created a dictionary called `my_dict` with three key-value pairs. We then use the items() method to extract all the key-value pairs from the dictionary and convert it into a list using the list() constructor. Finally, we print the list.
Conclusion
In this article, we have explored three methods to convert a Python dictionary into a list. You can use these methods according to your requirements. The keys() method will give you a list of all the keys present in the dictionary, while the values() method will give you a list of all the values present in the dictionary. The items() method will give you a list of tuples, where each tuple contains a key-value pair. With this knowledge, you can easily manipulate dictionaries into lists and vice versa.