Others have showed how you can unpack into a dict. However, in answer to your question "is there a better way", I would argue that:
d.update(a='foo',b='bar')
much easier to parse. Admitedtly, this doesn't work if you have a
and b
which are variables, but then you could use:
d.update({a:'foo',b:'bar'})
and I think I still prefer that version for the following reasons:
- It scales up to multiple (>2) values nicer as it can be broken onto multiple lines more cleanly
- It makes it immediately clear which key is associated with which value
And if you start off with a 2-tuple of values, rather than it being static as you show, you could even use zip
:
d.update( zip(("a","b"),("foo","bar")) )
which is admittedly not as nice as the other two options ...
... And we've just covered all 3 ways you can use dict.update
:).