How to convert a list to a dictionary with indexes as values?

PythonDictionary

Python Problem Overview


I am trying to convert the following list:

l = ['A', 'B', 'C']

To a dictionary like:

d = {'A': 0, 'B': 1, 'C': 2}

I have tried answers from other posts but none is working for me. I have the following code for now:

d = {l[i]: i for i in range(len(l))}

Which gives me this error:

unhashable type: 'list'

Python Solutions


Solution 1 - Python

You can get the indices of a list from the built-in enumerate. You just need to reverse the index-value map and use a dictionary comprehension to create a dictionary:

>>> lst = ['A', 'B', 'C']
>>> {k: v for v, k in enumerate(lst)}
{'A': 0, 'C': 2, 'B': 1}

Solution 2 - Python

Use built-in functions dict and zip:

>>> lst = ['A', 'B', 'C']
>>> dict(zip(lst, range(len(lst))))

Solution 3 - Python

Python dict constructor has an ability to convert list of tuple to dict, with key as first element of tuple and value as second element of tuple. To achieve this you can use builtin function enumerate which yield tuple of (index, value).

However question's requirement is exact opposite i.e. tuple should be (value, index). So this requires and additional step to reverse the tuple elements before passing to dict constructor. For this step we can use builtin reversed and apply it to each element of list using map

>>> lst = ['A', 'B', 'C']
>>> dict(map(reversed, enumerate(lst)))
>>> {'A': 0, 'C': 2, 'B': 1}

Solution 4 - Python

You can also take advantage of enumerate:

your_list = ['A', 'B', 'C']
your_dict = {key: i for i, key in enumerate(your_list)}

Solution 5 - Python

You have to convert the unhashable list into a tuple:

dct = {tuple(key): idx for idx, key in enumerate(lst)}

Solution 6 - Python

The easiest solution I used was:

lst = list('ABC')
dict(enumerate(lst))

edit: This is the reverse of what the author needed and exactly what I needed

Solution 7 - Python

  1. If the elements in target list are unique, then a dict comprehension should be enough and elegant, just like the accepted answer.

    >>> lst = ['A', 'B', 'C']
    >>> pos_map = {ele: pos for pos, ele in enumerate(lst)}
    
  2. But if there were duplicated elements in target list, then we could use the handy defaultdict in collections module:

    >>> lst = ['A', 'B', 'C', 'A', 'A', 'B']
    >>> from collections import defaultdict
    >>> pos_map = defaultdict(list)
    >>> for pos, ele in enumerate(lst):
    >>>     pos_map[ele].append(pos)
    >>>
    >>> pos_map
    >>> defaultdict(list, {'A': [0, 3, 4], 'B': [1, 5], 'C': [2]})
    

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionahajibView Question on Stackoverflow
Solution 1 - PythonAbhijitView Answer on Stackoverflow
Solution 2 - PythonflorexView Answer on Stackoverflow
Solution 3 - PythonSohaib FarooqiView Answer on Stackoverflow
Solution 4 - Pythongr1zzly be4rView Answer on Stackoverflow
Solution 5 - PythonDanielView Answer on Stackoverflow
Solution 6 - PythonChristo JosephView Answer on Stackoverflow
Solution 7 - PythonYaOzIView Answer on Stackoverflow