Search code examples
pythonlistdictionaryenumerate

How to create a value: index dict from a python list?


I have a list:

a = ['a', 'b', 'c']

I want to get the dict:

b = {'a':0, 'b':1, 'c':2}

What is the most pythonic way to get that?


Solution

  • Does the following help you?

    b = dict(map(lambda t: (t[1], t[0]), enumerate(a)))
    

    Example:

    >>> dict(map(lambda t: (t[1], t[0]), enumerate(['a', 'b', 'c'])))
    {'b': 1, 'c': 2, 'a': 0}