[BangPypers] Unpacking Tuple
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). Note: Number of values in the tuple will change dynamically. I know in python 3, you can do `a, b, c, *d = (1, 2, 3)` and then d will contain any elements that didn't fit into a,b,c. Regards, Saju ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] Unpacking Tuple
On 6 October 2012 15:39, Saju M wrote: > 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). > > Note: Number of values in the tuple will change dynamically. > Is the maximum length of the tuple fixed? If so, you can pad the tuple with "blank" variables to this maximum length, viz., NMAX = 4 UNKNOWN_VAL = '' t = (1, 2, 3) a, b, c, d = t + (NMAX - len(t)) * (UNKNOWN_VAL,) If the maximum length is not fixed, you should probably revisit your design. Regards, Gora ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] Unpacking Tuple
Saju M 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 ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] Unpacking Tuple
Something similar to Noufal's but with out using itertools... >>> s=(1,2,3,4) >>> a,b,c,d = s + (None,)*(4-len(s)) >>> print a,b,c,d 1 2 3 4 >>> s=(1,) >>> a,b,c,d = s + (None,)*(4-len(s)) >>> print a,b,c,d 1 None None None Regards Veerabahu On Sat, Oct 6, 2012 at 4:17 PM, Noufal Ibrahim wrote: > Saju M 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 > ___ > BangPypers mailing list > BangPypers@python.org > http://mail.python.org/mailman/listinfo/bangpypers > ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] Unpacking Tuple
All of these are clever but I'd be very annoyed if I saw them in serious production code. I'd recommend that you rethink your original code. If there are varying length tuples that you're passing in which you need assigned to variables, it might make more sense to use a dictionary. a, b, c and d will be keys. veerabahu subramanian writes: > Something similar to Noufal's but with out using itertools... > s=(1,2,3,4) a,b,c,d = s + (None,)*(4-len(s)) print a,b,c,d > 1 2 3 4 s=(1,) a,b,c,d = s + (None,)*(4-len(s)) print a,b,c,d > 1 None None None > > Regards > Veerabahu > > On Sat, Oct 6, 2012 at 4:17 PM, Noufal Ibrahim wrote: > >> Saju M 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 >> ___ >> BangPypers mailing list >> BangPypers@python.org >> http://mail.python.org/mailman/listinfo/bangpypers >> > ___ > BangPypers mailing list > BangPypers@python.org > http://mail.python.org/mailman/listinfo/bangpypers > -- Cordially, Noufal http://nibrahim.net.in ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers