# Initialize the dictionary my_dict = {'apple': 1, 'banana': 2, 'cherry': 3} # Convert the dictionary into a list my_list = [v for v in my_dict.values()] # Print the list print(my_list)
In this example, a dictionary called my_dict is initialized with three key-value pairs: 'apple': 1, 'banana': 2, and 'cherry': 3. This dictionary is then converted into a list called my_list using a list comprehension. The list comprehension generates a list of values from the dictionary (i.e., the values associated with the keys 'apple', 'banana', and 'cherry').
Note: This example uses a simple list comprehension to convert the dictionary into a list. Other approaches, such as using the values() method of the dictionary and the list() function, can also be used to achieve the same result.
Learn Flask development and learn to build cool apps with our premium Python course on Udemy.