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
Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Support Vector Machine (SVM) Algorithm Support Vector Machine (SVM) is a supervised machine learning algorithm used for classification and regression tasks. It tries to find the best boundary known as hyperplane that separates different classes in the data. It is useful when you want to do binary classification like spam vs. not spam or
9 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read