Map list onto dictionary

前端 未结 6 616
-上瘾入骨i
-上瘾入骨i 2021-02-02 08:14

Is there a way to map a list onto a dictionary? What I want to do is give it a function that will return the name of a key, and the value will be the original value. For example

6条回答
  •  旧时难觅i
    2021-02-02 08:54

    In Python 3 you can use this dictionary comprehension syntax:

    def foo(somelist):
        return {x[0]:x for x in somelist}
    

提交回复
热议问题