By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,766 Members | 1,317 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,766 IT Pros & Developers. It's quick & easy.

unpack tuple of wrong size

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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: an*****@despammed.com
Homepage: http://andres980.tripod.com/

"Well, well. Look-who's-BACK!"
-- Megatron
Jul 18 '05 #2

P: n/a
Tung Wai Yip <tu********@yahoo.com> wrote in
news:c7********************************@4ax.com:
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]

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.