python - How to unpack optional items from a tuple? -
this question has answer here:
i have list of input values, of first couple of mandatory , last couple optional. there easy way use tuple unpacking assign these variables, getting none if optional parameters missing.
eg.
a = [1,2] foo, bar, baz = # baz == none
ideally length - including longer 3 (other items thrown away).
at moment i'm using zip list of parameter names 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 bit long-winded.
from linked question, works:
foo, bar, baz == (list(a) + [none]*3)[:3]
Comments
Post a Comment