# unpack tuple of wrong size

Discussion in 'Python' started by Tung Wai Yip, Apr 6, 2004.

1. ### Tung Wai YipGuest

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

Tung Wai Yip, Apr 6, 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:
Homepage: http://andres980.tripod.com/

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

3. ### Duncan BoothGuest

Tung Wai Yip <> wrote in
news::

> 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]

Duncan Booth, Apr 7, 2004