Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Unpaking Tuple

Reply
Thread Tools

Unpaking Tuple

 
 
sajuptpm
Guest
Posts: n/a
 
      10-06-2012
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
 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      10-06-2012
On Sat, Oct 6, 2012 at 3:09 AM, sajuptpm <(E-Mail Removed)> 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.


Then you arguably want a list, not a tuple.

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]

Cheers,
Chris
 
Reply With Quote
 
 
 
 
Roy Smith
Guest
Posts: n/a
 
      10-06-2012
In article <(E-Mail Removed)>,
Chris Rebert <(E-Mail Removed)> 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]

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      10-06-2012
On Sat, 06 Oct 2012 08:46:28 -0400, Roy Smith wrote:

> In article <(E-Mail Removed)>,
> Chris Rebert <(E-Mail Removed)> 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]



While that's fine for small tuples, if somebody wanted to mess with you,
and passed (say) a million-item tuple, that would unnecessarily copy all
million items before throwing all but four away.

For the sake of a two-liner instead of a one-liner, you can avoid such
nasty surprises:

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

A similar solution:

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

Here's a dumb one-liner, good for making people laugh at you:

a,b,c,d = (x for i,x in enumerate(my_tuple + (None,)*4) if i < 4)

and an obfuscated solution:

from itertools import izip_longest as izip
a,b,c,d = (None if x == (None,None) else x[0][1] for x in izip(zip
('izip', my_tuple), (None for izip in 'izip')))


But in my opinion, the best solution of all is a three-liner:

if len(my_tuple) < 4:
my_tuple += (None,)*(4 - len(my_tuple))
a,b,c,d = my_tuple[4:]



--
Steven
 
Reply With Quote
 
woooee
Guest
Posts: n/a
 
      10-07-2012
On Oct 6, 3:09*am, sajuptpm <(E-Mail Removed)> wrote:
> 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).


Why is it necessary to unpack the tuple into arbitrary variables.
a_tuple=(1,2,3)
for v in a_tuple:
print v

for ctr in range(len(a_tuple)):
print a_tuple[ctr]
 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      10-07-2012
On 10/7/2012 1:58 PM, woooee wrote:
> On Oct 6, 3:09 am, sajuptpm <(E-Mail Removed)> wrote:
>> 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)


You cannot 'make' buggy code work -- except by changing it.
>>> a, b, c, *d = (1,2,3)
>>> d

[]

> Why is it necessary to unpack the tuple into arbitrary variables.


It is not necessary.

> a_tuple=(1,2,3)
> for v in a_tuple:
> print v


This is often the right way to access any iterable.

> for ctr in range(len(a_tuple)):
> print a_tuple[ctr]


This is seldom the right way. See the example below.

Unpacking is for when you have known-length iterables. For instance,
enumerate produces pairs.

>>> for i, v in enumerate('abc'):

print('Item {} is {}.'.format(i, v))

Item 0 is a.
Item 1 is b.
Item 2 is c.

--
Terry Jan Reedy

 
Reply With Quote
 
Thomas Bach
Guest
Posts: n/a
 
      10-08-2012
Hi there,

On Sat, Oct 06, 2012 at 03:08:38PM +0000, Steven D'Aprano wrote:
>
> my_tuple = my_tuple[:4]
> a,b,c,d = my_tuple if len(my_tuple) == 4 else (my_tuple + (None,)*4)[:4]
>


Are you sure this works as you expect? I just stumbled over the following:

$ python
Python 3.2.3 (default, Jun 25 2012, 23:10:56)
[GCC 4.7.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> split = ['foo', 'bar']
>>> head, tail = split if len(split) == 2 else split[0], None
>>> head

['foo', 'bar']
>>> tail
>>>


I don't get it! Could someone help me, please? Why is head not 'foo'
and tail not 'bar'?

Regards,
Thomas
 
Reply With Quote
 
Prasad, Ramit
Guest
Posts: n/a
 
      10-08-2012
Thomas Bach wrote:

> Hi there,
>
> On Sat, Oct 06, 2012 at 03:08:38PM +0000, Steven D'Aprano wrote:

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

>
> Are you sure this works as you expect? I just stumbled over the following:
>
> $ python
> Python 3.2.3 (default, Jun 25 2012, 23:10:56)
> [GCC 4.7.1] on linux2
> Type "help", "copyright", "credits" or "license" for more information.

> >>> split = ['foo', 'bar']
> >>> head, tail= split if len(split) == 2 else split[0], None
> >>> head

> ['foo', 'bar']

> >>> tail
> >>>

>
> I don't get it! Could someone help me, please? Why is head not 'foo'
> and tail not 'bar'?
>
> Regards,
> Thomas
> --


I think you just need to wrap the else in parenthesis so the
else clause is treated as a tuple. Without the parenthesis
I believe it is grouping the code like this.

head, tail = (split if len(split) == 2else split[0] ), None

You want:
head, tail = split if len(split) == 2 else (split[0], None )


Ramit
This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.
 
Reply With Quote
 
Bob Martin
Guest
Posts: n/a
 
      10-09-2012
in 682592 20121008 232126 "Prasad, Ramit" <(E-Mail Removed)> wrote:
>Thomas Bach wrote:=0D=0A> Hi there,=0D=0A> =0D=0A> On Sat, Oct 06, 2012 at =
>03:08:38PM +0000, Steven D'Aprano wrote:=0D=0A> >=0D=0A> > my_tuple =3D my_=
>tuple[:4]=0D=0A> > a,b,c,d =3D my_tuple if len(my_tuple) =3D=3D 4 else (my_=
>tuple + (None,)*4)[:4]=0D=0A> >=0D=0A> =0D=0A> Are you sure this works as y=
>ou expect? I just stumbled over the following:=0D=0A> =0D=0A> $ python=0D=
>=0A> Python 3=2E2=2E3 (default, Jun 25 2012, 23:10:56)=0D=0A> [GCC 4=2E7=2E=
>1] on linux2=0D=0A> Type "help", "copyright", "credits" or "license" for mo=
>re information=2E=0D=0A> >>> split =3D ['foo', 'bar']=0D=0A> >>> head, tail=
>=3D split if len(split) =3D=3D 2 else split[0], None=0D=0A> >>> head=0D=0A=
>> ['foo', 'bar']=0D=0A> >>> tail=0D=0A> >>>=0D=0A> =0D=0A> I don't get it! =

>Could someone help me, please? Why is head not 'foo'=0D=0A> and tail not 'b=
>ar'?=0D=0A> =0D=0A> Regards,=0D=0A> Thomas=0D=0A> --=0D=0A=0D=0AI think yo=
>u just need to wrap the else in parenthesis so the=0D=0Aelse clause is trea=
>ted as a tuple=2E Without the parenthesis =0D=0AI believe it is grouping th=
>e code like this=2E=0D=0A=0D=0Ahead, tail =3D (split if len(split) =3D=3D 2=
>else split[0] ), None=0D=0A=0D=0AYou want:=0D=0Ahead, tail =3D split if le=
>n(split) =3D=3D 2 else (split[0], None )=0D=0A=0D=0A=0D=0ARamit=0D=0AThis e=
>mail is confidential and subject to important disclaimers and=0D=0Aconditio=
>ns including on offers for the purchase or sale of=0D=0Asecurities, accurac=
>y and completeness of information, viruses,=0D=0Aconfidentiality, legal pri=
>vilege, and legal entity disclaimers,=0D=0Aavailable at http://www=2Ejpmorg=
>an=2Ecom/pages/disclosures/email=2E


How does one unpack this post?
 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      10-09-2012
On 10/09/2012 02:07 AM, Bob Martin wrote:
> in 682592 20121008 232126 "Prasad, Ramit" <(E-Mail Removed)> wrote:
>> Thomas Bach wrote:=0D=0A> Hi there,=0D=0A> =0D=0A> On Sat, Oct 06, 2012 at =
>> 03:08:38PM +0000, Steven D'Aprano wrote:=0D=0A> >=0D=0A> > my_tuple =3D my_=
>> tuple[:4]=0D=0A> > a,b,c,d =3D my_tuple if len(my_tuple) =3D=3D 4 else (my_=
>> <SNIP>
>> y and completeness of information, viruses,=0D=0Aconfidentiality, legal pri=
>> vilege, and legal entity disclaimers,=0D=0Aavailable at http://www=2Ejpmorg=
>> an=2Ecom/pages/disclosures/email=2E

> How does one unpack this post?


Since that's not the way it arrived here, i have to ask, how do you get
these posts? Are you subscribed to individual messages by email via
python.org? or are you using google-groups or some other indirection?

In any reasonable mail program, you can see the source of a message.
Most of the troubles i've seen here have been caused by people trying to
send html mail to a text-based mailing list. But in the case you quote,
the original message came here as text/plain, and well formatted.

--

DaveA

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
unpaking sequences of unknown length Anthra Norell Python 4 08-28-2006 07:06 AM
Why tuple with one item is no tuple Gregor Horvath Python 37 03-30-2005 06:58 AM
Easily convert unicode tuple to python string tuple??? Michal Mikolajczyk Python 1 04-20-2004 08:37 PM
Re: Easily convert unicode tuple to python string tuple??? Jeff Epler Python 0 04-20-2004 03:36 PM
Re: Easily convert unicode tuple to python string tuple??? Bill Scherer Python 0 04-20-2004 03:34 PM



Advertisments