Python dict: unpacking some value and collect the rest like JS?

  iterable-unpacking, python

In Javascript, we can do const {a, b, ...rest} = {a: 10, b: 20, c: 30, d: 40};, and rest will be {c: 30, d: 40}.

Is there any way to do this in Python?

I can use itemgetter to achieve like

dic = {'a': 10, 'b': 20, 'c': 30, 'd': 40}
a, b = itemgetter('a', 'b')(dic)

How can we collect the rest key-val pairs to rest?

Source: Python Questions

LEAVE A COMMENT