Python: Convert list to dictionary with indexes as values

后端 未结 5 1961
傲寒
傲寒 2020-12-02 16:08

I am trying to convert the following list:

list = [\'A\',\'B\',\'C\']

To a dictionary like:

dict = {\'A\':0, \'B\':1, \'C\'         


        
5条回答
  •  情深已故
    2020-12-02 17:09

    Don't use list as your variable name because it's reserved by Python. You can also take advantage of enumerate.

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

提交回复
热议问题