# Initialize the list my_list = ['apple', 'banana', 'cherry']
# Convert the list into a dictionary
my_dict = {i: my_list[i] for i in range(len(my_list))}
# Print the dictionary print(my_dict)
In this example, a list called my_list is initialized with three strings: 'apple', 'banana', and 'cherry'. This list is then converted into a dictionary called my_dict using a dictionary comprehension. The keys of the dictionary are the indices of the items in the list, and the values are the items themselves.
Note: This example uses a simple dictionary comprehension to convert the list into a dictionary. Other approaches, such as using the dict() function, can also be used to achieve the same result.
Practice what you just learned
Solve Python exercises and get instant AI feedback on your solutions.
Try ActiveSkill for Free →
Python Mega Course: Learn Python in 60 Days, Build 20 Apps
Learn Python on Udemy completely in 60 days or less by building 20 real-world applications from web development to data science.
