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

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -