125

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'
6
  • your code works for me.
    – Daniel
    Apr 6, 2016 at 18:57
  • 6
    1. Don't use list as variable name 2. Use enumerate as shown {j:i for i,j in enumerate(l)} Apr 6, 2016 at 18:57
  • 1
    You have a bug in code you haven't shown us. The elements of list are probably lists, although you could have made other mistakes, like writing list: i instead of list[i]: i. Apr 6, 2016 at 19:01
  • @user2357112 I edited the post. You are correct that makes a lot of difference.
    – ahajib
    Apr 6, 2016 at 19:03
  • Strange that no one noticed what if there are duplicate elements
    – EXODIA
    Oct 31, 2020 at 8:36

7 Answers 7

185

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}
1
  • 3
    When using this solution, be aware that duplicates in the original list will result in a single key with the highest value available. Aug 26, 2022 at 16:36
35

Use built-in functions dict and zip:

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

You can also take advantage of enumerate:

your_list = ['A', 'B', 'C']
your_dict = {key: i for i, key in enumerate(your_list)}
4
  • 1
    why don't you use the item of your for-loop?
    – Daniel
    Apr 6, 2016 at 18:58
  • Because you don't have to. I still changed it. Apr 6, 2016 at 19:03
  • Just like list is reserved, dict is too. They're the list and dictionary types, respectively. Jan 23, 2021 at 23:52
  • Wow - almost 5 years later and I never realized ha - updated, thanks Jan 24, 2021 at 0:15
10

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}
2
  • 1
    This version seems to take longer than the other solutions provided Aug 12, 2020 at 16:12
  • A short solution is nice, but at a cursory glance, it's not clear how reversed() is being used. Because of the extra function calls to map() and reversed(), it also runs slower than a dictionary comprehension. Aug 26, 2022 at 16:29
4
  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]})
    
0

You have to convert the unhashable list into a tuple:

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

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

2
  • 3
    This will give you {0: 'A', 1: 'B', 2: 'C'} which is different from the wanted result {'A':0, 'B':1, 'C':2}
    – MadMike
    Apr 19, 2021 at 7:03
  • 1
    Which is also really not necessary. Why have a dict whose keys are sequential integers? It is just a list for all purposes...
    – Tomerikoo
    Dec 2, 2021 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.