python - How to unpack optional items from a tuple? -


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

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -