How to unpack optional items from a tuple?

iterable-unpacking, python, tuples

Solution

From the linked question, this works:

foo, bar, baz == (list(a) + [None]*3)[:3]

Problem

I have a list of some input values, of which the first couple of mandatory and the last couple optional. Is there any easy way to use tuple unpacking to assign these to variables, getting None if the optional parameters are missing. eg. ``` a = [1,2] foo, bar, baz = a # baz == None ``` ideally a could be any length - including longer than 3 (other items thrown away). At the moment I'm using zip with a list of parameter names to get a dictionary: ``` items = dict(zip(('foo', 'bar', 'baz'), a)) foo = items.get('foo', None) bar = items.get('bar', None) baz = items.get('baz', None) ``` but it's a bit long-winded.

Original source

Related problems