[BangPypers] Unpacking Tuple

Noufal Ibrahim noufal at nibrahim.net.in
Sat Oct 6 12:47:28 CEST 2012


Saju M <sajuptpm at gmail.com> writes:

> Hi,
>
> I am using python 2.6.
>
> I need a way to make following code working without any ValueError .
>>>> a, b, c, d = (1,2,3,4)
>>>> a, b, c, d = (1,2,3).

This is crude but fun. I wouldn't do it in anything serious program though.

> import itertools
>
> a, b, c, d = list(itertools.chain(input_tuple, itertools.repeat(None, 4)))[:4]

If your input tuple has 4 values, you'll get them in the 4 variables. 

If your input tuple has less (say 3), you'll get a, b and c. d will be
set to None.

If your input tuple has more than 4, the remaining will be ignored. 

-- 
Cordially,
Noufal
http://nibrahim.net.in


More information about the BangPypers mailing list