unpack tuple of wrong size

T

Tung Wai Yip

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
 
A

Andres Rosado-Sepulveda

Tung said:
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: (e-mail address removed)
Homepage: http://andres980.tripod.com/

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

Duncan Booth

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]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,165
Latest member
JavierBrak
Top