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}
More Related Contents:
- Creating multiple python dictionaries from a file
- Why can’t I use a list as a dict key in python?
- How can I get list of values from dict?
- Creating a list of dictionaries results in a list of copies of the same dictionary
- heapq with custom compare predicate
- Is a Python dictionary an example of a hash table?
- how to iterate through dictionary in a dictionary in django template?
- How to check if one dictionary is a subset of another larger dictionary?
- Subclassing Python dictionary to override __setitem__
- Check if value already exists within list of dictionaries?
- Convert list into a dictionary [duplicate]
- Sort nested dictionary by value, and remainder by another value, in Python
- switching keys and values in a dictionary in python [duplicate]
- OrderedDict vs defaultdict vs dict [closed]
- Most Pythonic Way to Build Dictionary From Single List
- How can I update an attribute created by a base class’ mutable default argument, without modifying that argument? [duplicate]
- How to pass dictionary items as function arguments in python? [duplicate]
- scipy.io.loadmat nested structures (i.e. dictionaries)
- How to sort dictionary by key in numerical order Python
- Initialize List to a variable in a Dictionary inside a loop
- Iterating through a JSON object
- Appending to list in Python dictionary [duplicate]
- Converting xml to dictionary using ElementTree
- Using Python class as a data container [closed]
- How can I convert string values from a dictionary, into int/float datatypes?
- How to type hint a dictionary with values of different types
- sorting list of nested dictionaries in python
- How to convert string representation of dictionary in Pandas DataFrame to a new columns?
- How to convert only numbers in a mixed list into float
- In what case would I use a tuple as a dictionary key? [closed]