Convert a list to a dictionary in Python

前端 未结 12 2195
無奈伤痛
無奈伤痛 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:13

    {x: a[a.index(x)+1] for x in a if a.index(x) % 2 ==0}
    
    result : {'hello': 'world', '1': '2'}
    

提交回复
热议问题