Convert a Dictionary to a List of Values in Python
To convert a dictionary to a list of values in Python, you can use the values()
method, which returns a view object of all dictionary values. This view object can be easily converted into a list using the list()
constructor.
Examples
1. Dictionary to a List of Values using the values()
Method
The simplest way to convert a dictionary into a list of values is by using the values()
method.
# Creating a dictionary
student_scores = {"Alice": 85, "Bob": 90, "Charlie": 78}
# Converting dictionary values to a list
values_list = list(student_scores.values())
# Printing the list of values
print("List of Values:", values_list)
Explanation:
In this example:
- The dictionary
student_scores
stores names as keys and their scores as values. - We use
student_scores.values()
to retrieve all values from the dictionary. - The
list()
constructor converts these values into a list.
Output:
List of Values: [85, 90, 78]
2. Extracting Values from a Dictionary with Different Data Types
Dictionary values can have different data types, such as integers, strings, and lists.
# Creating a dictionary with mixed data types
person_info = {"name": "John", "age": 30, "hobbies": ["reading", "cycling"]}
# Converting dictionary values to a list
values_list = list(person_info.values())
# Printing the list of values
print("List of Values:", values_list)
Explanation:
In this example:
- The dictionary
person_info
contains a string, an integer, and a list as values. - We retrieve all values using
person_info.values()
. - The
list()
function converts these values into a list.
Output:
List of Values: ['John', 30, ['reading', 'cycling']]
3. Converting Nested Dictionary Values to a List
When dealing with nested dictionaries, we may need to extract values from inner dictionaries.
# Creating a nested dictionary
employee_data = {
"emp1": {"name": "Alice", "salary": 5000},
"emp2": {"name": "Bob", "salary": 6000},
}
# Extracting only the salaries from the nested dictionary
salaries = [data["salary"] for data in employee_data.values()]
# Printing the extracted list of salaries
print("List of Salaries:", salaries)
Explanation:
In this example:
- The dictionary
employee_data
stores employee records, where each key holds a dictionary. - We iterate through
employee_data.values()
and extract thesalary
from each inner dictionary using list comprehension. - The result is a list containing all salaries.
Output:
List of Salaries: [5000, 6000]
4. Converting Dictionary Values to a Sorted List
We can sort the extracted values in ascending or descending order.
# Creating a dictionary
product_prices = {"Laptop": 1200, "Phone": 800, "Tablet": 500}
# Converting dictionary values to a sorted list
sorted_prices = sorted(product_prices.values())
# Printing the sorted list of values
print("Sorted Prices:", sorted_prices)
Explanation:
In this example:
- The dictionary
product_prices
stores product names as keys and their prices as values. - We use
sorted()
to sort the extracted values in ascending order.
Output:
Sorted Prices: [500, 800, 1200]
Conclusion
Python provides multiple ways to convert dictionary values into a list:
- Using
values()
: The easiest method to retrieve all dictionary values. - Handling mixed data types: Ensures all values, regardless of type, are stored in a list.
- Extracting from nested dictionaries: Uses list comprehension for targeted extraction.
- Sorting dictionary values: Helps retrieve values in a specific order.
By selecting the appropriate method, you can efficiently work with dictionary values as lists.