How to convert a list to a dictionary in Python

Converting a list to a dictionary results in a dictionary where the keys and values are taken from the even and odd positions of the list, respectively.

Solution for How to convert a list to a dictionary in Python : You can use the in keyword to convert a list to a dictionary Use a for-loop and range(start, stop, step) to iterate over a range from start to stop where stop is len(list) and step is 2. Map each key to a value in the dictionary where each key is an even indexed element and each value is the following odd indexed element.

Use a dictionary comprehension for a more compact implementation.

a_list = ["a", 1, "b", 2, "c", 3]


how-to-convert-a-list-to-a-dictionary-in-python