[Python-ideas] Unpack of sequences

Steven D'Aprano steve at pearwood.info
Wed Aug 29 19:18:12 CEST 2012

On 30/08/12 03:01, Steven D'Aprano wrote:

> a, b, x, y = **mapping
> could be equivalent to:
> a, b, x, y = mapping['a'], mapping['b'], mapping['x'], mapping['y']

Oh, I forgot to mention... extra keys are ignored. That's because in
my experience, you are likely to have a mapping with many different keys
(say, a set of config options in a dict) and you only want to extract a
few at a time. Unlike unpacking a tuple, you're unlikely to need *every*
field at once.

A potentially useful extension to the idea is to capture the extra
items in a dict:

a, b, x, y, **extras = **mapping

which is like:

a, b, x, y = [mapping[name] for name in ('a', 'b', 'x', 'y')]
extras = dict((k, v) for k,v in mapping.items() if k not in ('a', 'b', 'x', 'y'))


More information about the Python-ideas mailing list