Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > unpack tuple of wrong size

Reply
Thread Tools

unpack tuple of wrong size

 
 
Tung Wai Yip
Guest
Posts: n/a
 
      04-06-2004
I want to do

t = (1,2)
a,b = t # get a=1 and b=2

However when
t = (1,)
a,b=t

I got a "ValueError: unpack tuple of wrong size"

What I want is for a=1 and b=None. Is there a good way to do this?


Wai Yip Tung
 
Reply With Quote
 
 
 
 
Andres Rosado-Sepulveda
Guest
Posts: n/a
 
      04-06-2004
Tung Wai Yip wrote:

> I want to do
>
> t = (1,2)
> a,b = t # get a=1 and b=2
>
> However when
> t = (1,)
> a,b=t
>
> I got a "ValueError: unpack tuple of wrong size"
>
> What I want is for a=1 and b=None. Is there a good way to do this?


t = (1,None)
a,b = t

(1,) means that the tuple has only one element. Remember that tuples are
defined by the comma, except on those cases where it would be unclear
what the intention is.

--
Andres Rosado
Email: http://www.velocityreviews.com/forums/(E-Mail Removed)
Homepage: http://andres980.tripod.com/

"Well, well. Look-who's-BACK!"
-- Megatron
 
Reply With Quote
 
 
 
 
Duncan Booth
Guest
Posts: n/a
 
      04-07-2004
Tung Wai Yip <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> However when
> t = (1,)
> a,b=t
>
> I got a "ValueError: unpack tuple of wrong size"
>
> What I want is for a=1 and b=None. Is there a good way to do this?


Probably the simplest is:

a, b = (t + (None, None))[:2]

 
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
*tuple vs tuple example print os.path.join(os.path.dirname(os.tmpnam()),*("a","b","c")) Steve Python 1 12-13-2005 10:25 PM
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