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

后端 未结 5 1439
耶瑟儿~
耶瑟儿~ 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条回答
  •  -上瘾入骨i
    2020-12-06 01:55

    You can zip the lists of alternate elements like so

    >>> lst = ["a", 1, "b", 2, "c", 3]
    >>> dict(zip(lst[::2], lst[1::2])
    {'a': 1, 'c': 3, 'b': 2}
    

提交回复
热议问题