Destructuring-bind dictionary contents

前端 未结 12 1110
無奈伤痛
無奈伤痛 2020-12-02 15:24

I am trying to \'destructure\' a dictionary and associate values with variables names after its keys. Something like

params = {\'a\':1,\'b\':2}
a,b = params.         


        
12条回答
  •  我在风中等你
    2020-12-02 15:44

    from operator import itemgetter
    
    params = {'a': 1, 'b': 2}
    
    a, b = itemgetter('a', 'b')(params)
    

    Instead of elaborate lambda functions or dictionary comprehension, may as well use a built in library.

提交回复
热议问题