Convert a Dictionary to a List in Python
In Python, dictionaries and lists are important data structures. Dictionaries hold pairs of keys and values, while lists are groups of elements arranged in a specific order. Sometimes, you might want to change a dictionary into a list, and Python offers various ways to do this.
How to Convert a Dictionary to a List in Python
Below, are the ways to Convert a Dictionary to a List in Python.
- Using ‘items()’ Method
- Using List Comprehension
- Using zip() Function
- Using * Operator
Convert a Dictionary to a List Using ‘items()’ Method
In this example, the ‘items()’ method is used to retrieve key-value pairs from the dictionary, and then ‘list()’ is used to convert those pairs into a list of tuples.
Python3
# Sample Dictionary sample_dict = { 'a' : 1 , 'b' : 2 , 'c' : 3 } # Convert Dictionary to List using items() list_from_dict = list (sample_dict.items()) # Display the result print ( type (sample_dict)) print ( "Converted List:" , list_from_dict) print ( type (list_from_dict)) |
<class 'dict'> Converted List: [('a', 1), ('b', 2), ('c', 3)] <class 'list'>
Convert a Dictionary to a List Using List Comprehension
In this example, a list comprehension is employed to iterate over the key-value pairs obtained from ‘items()’ and create a list of tuples.
Python3
# Sample Dictionary sample_dict = { 'a' : 1 , 'b' : 2 , 'c' : 3 } # Convert Dictionary to List using list comprehension list_from_dict = [(key, value) for key, value in sample_dict.items()] # Display the result print ( type (sample_dict)) print ( "Converted List:" , list_from_dict) print ( type (list_from_dict)) |
<class 'dict'> Converted List: [('a', 1), ('b', 2), ('c', 3)] <class 'list'>
Convert a Dictionary to a List Using zip() Function
In this example, below code uses the zip()
function to convert a dictionary (sample_dict
) into a list of tuples containing key-value pairs, and then prints the types of the original dictionary, the converted list.
Python3
# Sample Dictionary sample_dict = { 'a' : 1 , 'b' : 2 , 'c' : 3 } # Convert Dictionary to List using zip() list_from_dict = list ( zip (sample_dict.keys(), sample_dict.values())) # Display the result print ( type (sample_dict)) print ( "Converted List:" , list_from_dict) print ( type (list_from_dict)) |
<class 'dict'> Converted List: [('a', 1), ('b', 2), ('c', 3)] <class 'list'>
Convert a Dictionary to a List Using * Operator
Python’s dictionary unpacking feature, using the `*` operator, allows for a concise conversion of a dictionary to a list.
Python3
# Sample Dictionary sample_dict = { 'a' : 1 , 'b' : 2 , 'c' : 3 } # Converting to List using Dictionary unpacking list_from_dict = [ * sample_dict.items()] # Output print ( type (sample_dict)) print ( "Converted List:" , list_from_dict) print ( type (list_from_dict)) |
<class 'dict'> Converted List: [('a', 1), ('b', 2), ('c', 3)] <class 'list'>
Conclusion
In conclusion, converting a dictionary to a list in Python is a straightforward process with multiple approaches. Depending on your specific use case, you can choose between extracting keys, values, or key-value pairs. Whether you prefer the simplicity of the `list()` function, the elegance of list comprehensions, or the versatility of the `zip()` function, these methods provide the flexibility needed to adapt your data structures to various programming scenarios.
Contact Us