Python: how to build a dict from plain list of keys and values

后端 未结 5 1450
耶瑟儿~
耶瑟儿~ 2020-12-06 01:22

I have a list of values like:

[\"a\", 1, \"b\", 2, \"c\", 3]

and I would like to build such a dict from it:

{\"a\": 1, \"b\         


        
5条回答
  •  不知归路
    2020-12-06 01:51

    I don't really see many situations where you would run into this exact problem, so there is no 'natural' solution. A quick one liner that should do the trick for you would be however:

       {input_list[2*i]:input_list[2*i+1] for i in range(len(input_list)//2)}
    

提交回复
热议问题