Assign contents of Python dict to multiple variables at once?
dictionary, python
Solution
It wouldn't make any sense for unpacking to depend on the variable names. The closest you can get is:
a, b = [f()[k] for k in ('a', 'b')]
This, of course, evaluates `f()` twice.
You could write a function:
def unpack(d, *keys)
return tuple(d[k] for k in keys)
Then do:
a, b = unpack(f(), 'a', 'b')
This is really all overkill though. Something simple would be better:
result = f()
a, b = result['a'], result['b']
Problem
I would like to do something like this ``` def f(): return { 'a' : 1, 'b' : 2, 'c' : 3 } { a, b } = f() # or { 'a', 'b' } = f() ? ``` I.e. so that a gets assigned 1, b gets 2, and c is undefined This is similar to this ``` def f() return( 1,2 ) a,b = f() ```