Python Convert Dictionary to List of Values
Last Updated :
07 Feb, 2024
Python has different types of built-in data structures to manage your data. A list is a collection of ordered items, whereas a dictionary is a key-value pair data. Both of them are unique in their own way. In this article, the dictionary is converted into a list of values in Python using various concepts and methods. Here, we will see how we can convert a dictionary into a list of values in Python.
Convert Dictionary to List of Values in Python
Below are some of the ways by which we can convert a dictionary to a list of values in Python:
- Using for loop
- Using items() Function
- Using Zip Function
Convert Dictionary to List Using For Loop
The below method converts dictionary data into a list list_of_data using a for loop. It iterates through the keys of the dictionary, retrieves the corresponding values using data.get(key), and appends a list containing the key-value pair to the list_of_data list.
Python
# initial dictionary
data = {1: "Gaurav", 2: "Sanket", 3: "Anjali", 4: "Priyanka"}
# empty list to store key-value pairs
list_of_data = []
# iterate through dictionary
for key in data:
# append key-value pair to the list
list_of_data.append([key, data.get(key)])
# print the resulting list
print(type(list_of_data))
print(list_of_data)
Output<type 'list'>
[[1, 'Gaurav'], [2, 'Sanket'], [3, 'Anjali'], [4, 'Priyanka']]
Python Convert Dictionary to List Using .items() Function
The below method uses the items() method to convert a dictionary (data) into a list (list_of_data), containing key-value pairs. The resulting list is then printed, demonstrating the transformation of dictionary data into a list of values.
Python
# initial dictionary
data = {1: "Gaurav", 2: "Sanket", 3: "Anjali", 4: "Priyanka"}
# convert dict items into list
list_of_data = list(data.items())
# printing the resulting list
print(type(list_of_data))
print(list_of_data)
Output<type 'list'>
[(1, 'Gaurav'), (2, 'Sanket'), (3, 'Anjali'), (4, 'Priyanka')]
Python Dictionary to List Using Zip Function
Using the zip function, the provided code converts a dictionary (data) into a list (list_of_data) by combining keys and values into tuples and then converting them into a list. This method showcases the demonstration of the zip function for creating a list of values from a dictionary.
Python
# initial dictionary
data = {1: "Gaurav", 2: "Sanket", 3: "Anjali", 4: "Priyanka"}
# Zip tuple of kays & values into list
list_of_data = list(zip(data.keys(), data.values()))
# printing the resulting list
print(type(list_of_data))
print(list_of_data)
Output<type 'list'>
[(1, 'Gaurav'), (2, 'Sanket'), (3, 'Anjali'), (4, 'Priyanka')]
Conclusion
In conclusion, various methods have been explored for converting a dictionary to a list of values in Python. Whether using for loops, the .items() function, list comprehension, the zip function, or the map function, each approach offers a way to achieve the conversion. Developers can choose the method that best fits their preferences and coding style, enhancing flexibility in managing and manipulating data structures.
Similar Reads
Convert Dictionary to List of Tuples - Python Converting a dictionary into a list of tuples involves transforming each key-value pair into a tuple, where the key is the first element and the corresponding value is the second. For example, given a dictionary d = {'a': 1, 'b': 2, 'c': 3}, the expected output after conversion is [('a', 1), ('b', 2
3 min read
Python - Convert dictionary items to values Sometimes, while working with Python dictionary, we can have a problem in which we need to convert all the items of dictionary to a separate value dictionary. This problem can occur in applications in which we receive dictionary in which both keys and values need to be mapped as separate values. Let
3 min read
Python | List of tuples to dictionary conversion Interconversions are always required while coding in Python, also because of the expansion of Python as a prime language in the field of Data Science. This article discusses yet another problem that converts to dictionary and assigns keys as 1st element of tuple and rest as it's value. Let's discuss
3 min read
Python - Add Values to Dictionary of List A dictionary of lists allows storing grouped values under specific keys. For example, in a = {'x': [10, 20]}, the key 'x' maps to the list [10, 20]. To add values like 30 to this list, we use efficient methods to update the dictionary dynamically. Letâs look at some commonly used methods to efficien
3 min read
Python - Convert key-values list to flat dictionary We are given a list that contains tuples with the pairs of key and values we need to convert that list into a flat dictionary. For example a = [("name", "Ak"), ("age", 25), ("city", "NYC")] is a list we need to convert it to dictionary so that output should be a flat dictionary {'name': 'Ak', 'age':
3 min read