Map list onto dictionary

前端 未结 6 599
-上瘾入骨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条回答
  •  隐瞒了意图╮
    2021-02-02 08:43

    Taking hints from other answers I achieved this using map operation. I am not sure if this exactly answers your question.

    mylist = ["hello", "world"]
    def convert_to_dict( somelist ):
        return dict( map( lambda x: (x[0], x), somelist ) )
    
    final_ans = convert_to_dict( mylist ) 
    print final_ans
    

提交回复
热议问题