How to create a dict from a list in the format {index : value}?

arrays, dictionary, python

Solution

You can use dict comprehension for this purpose!

>>> d= [0.41,1.87,1.10,7.05]
>>> {index:i for index,i in enumerate(d)}
{0: 0.41, 1: 1.87, 2: 1.1, 3: 7.05}

Problem

Assume this list: ``` a = [0.41 1.87 1.10 7.05] ``` I'd like to build a dict that would look as follows: ``` d = {0: 0.41, 1: 1.87, 2: 1.10, 3: 7.05} ``` There are numerous answers on SO on how to convert from a list to a dict, however I could not find one that addresses this specific need (i.e. having the key value of dict being the element's index of the list). I can imaging there are cumbersome ways of doing this, such a using a `for` loop, getting the `a.index(i)`, appending, zipping etc but I wonder if I'm missing on a more efficient way of building this dictionary.

Original source