Convert a Dictionary to a List in Python
Last Updated :
07 Feb, 2024
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.
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))
Output<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))
Output<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))
Output<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))
Output<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.
Similar Reads
Convert Two Lists into a Dictionary - Python We are given two lists, we need to convert both of the list into dictionary. For example we are given two lists a = ["name", "age", "city"], b = ["Geeks", 30,"Delhi"], we need to convert these two list into a form of dictionary so that the output should be like {'name': 'Geeks', 'age': 30, 'city': '
3 min read
Convert a list of Tuples into Dictionary - Python Converting a list of tuples into a dictionary involves transforming each tuple, where the first element serves as the key and the second as the corresponding value. For example, given a list of tuples a = [("a", 1), ("b", 2), ("c", 3)], we need to convert it into a dictionary. Since each key-value p
3 min read
Python - Convert list of dictionaries to JSON In this article, we will discuss how to convert a list of dictionaries to JSON in Python. Python Convert List of Dictionaries to JsonBelow are the ways by which we can convert a list of dictionaries to JSON in Python: Using json.dumps()Using json.dump()Using json.JSONEncoderUsing default ParameterDi
5 min read
Python - Converting list string to dictionary Converting a list string to a dictionary in Python involves mapping elements from the list to key-value pairs. A common approach is pairing consecutive elements, where one element becomes the key and the next becomes the value. This results in a dictionary where each pair is represented as a key-val
3 min read
How To Convert Python Dictionary To JSON? In Python, a dictionary stores information using key-value pairs. But if we want to save this data to a file, share it with others, or send it over the internet then we need to convert it into a format that computers can easily understand. JSON (JavaScript Object Notation) is a simple format used fo
6 min read