Converting dictionary values to a list allows you to extract the values and store them as separate elements. In this blog post, we will explore different methods to convert dictionary values to a list in Python.
Method 1: Using the values() Method
# Method 1: Using the values() Method my_dict = {"name": "John", "age": 30, "city": "New York"} value_list = list(my_dict.values()) print("Values as list:", value_list)
Output:
Values as list: [‘John’, 30, ‘New York’]
Method 2: Using Dictionary Comprehension
# Method 2: Using Dictionary Comprehension my_dict = {"name": "John", "age": 30, "city": "New York"} value_list = [value for value in my_dict.values()] print("Values as list:", value_list)
Output:
Values as list: [‘John’, 30, ‘New York’]