Unpaking Tuple

Roy Smith roy at panix.com
Sat Oct 6 08:46:28 EDT 2012


In article <mailman.1898.1349519275.27098.python-list at python.org>,
 Chris Rebert <clp2 at rebertia.com> wrote:

> But at any rate:
> shortfall = 4 - len(your_tuple)
> your_tuple += (None,) * shortfall # assuming None is a suitable default
> a, b, c, d = your_tuple
> 
> If you also need to handle the "too many items" case, use slicing:
> a, b, c, d = your_tuple[:4]

I usually handle both of those cases at the same time:

>>> a, b, c, d = (my_tuple + (None,) * 4)[:4]



More information about the Python-list mailing list