Convert a list to a dictionary in Python

前端 未结 12 1995
無奈伤痛
無奈伤痛 2020-11-22 04:14

Let\'s say I have a list a in Python whose entries conveniently map to a dictionary. Each even element represents the key to the dictionary, and the following o

12条回答
  •  一向
    一向 (楼主)
    2020-11-22 05:06

    I am not sure if this is pythonic, but seems to work

    def alternate_list(a):
       return a[::2], a[1::2]
    
    key_list,value_list = alternate_list(a)
    b = dict(zip(key_list,value_list))
    

提交回复
热议问题