Velocity Reviews > unpack tuple of wrong size

# 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

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.

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

"Well, well. Look-who's-BACK!"
-- Megatron

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]