Assign contents of Python dict to multiple variables at once?

前端 未结 5 2280
滥情空心
滥情空心 2021-02-20 07:41

I would like to do something like this

def f():
    return { \'a\' : 1, \'b\' : 2, \'c\' : 3 }

{ a, b } = f()     # or { \'a\', \'b\' } = f() ?
<
5条回答
  •  一个人的身影
    2021-02-20 08:05

    mydict = {'aa':2, 'bb':'john', 'cc':34, 'dd':'bye'}
    
    a, b, c, d = [mydict[k] for k in ['aa', 'bb', 'cc', 'dd']]
    

提交回复
热议问题