unpack tuple of wrong size

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

  1. Tung Wai Yip

    Tung Wai Yip Guest

    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
    #1
    1. Advertising

  2. 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:
    Homepage: http://andres980.tripod.com/

    "Well, well. Look-who's-BACK!"
    -- Megatron
    Andres Rosado-Sepulveda, Apr 6, 2004
    #2
    1. Advertising

  3. Tung Wai Yip

    Duncan Booth Guest

    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
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michal Mikolajczyk
    Replies:
    1
    Views:
    792
    Larry Bates
    Apr 20, 2004
  2. Jeff Epler
    Replies:
    0
    Views:
    924
    Jeff Epler
    Apr 20, 2004
  3. Bill Scherer
    Replies:
    0
    Views:
    602
    Bill Scherer
    Apr 20, 2004
  4. Gregor Horvath

    Why tuple with one item is no tuple

    Gregor Horvath, Mar 15, 2005, in forum: Python
    Replies:
    37
    Views:
    793
    Antoon Pardon
    Mar 30, 2005
  5. Steve
    Replies:
    1
    Views:
    945
    Fredrik Lundh
    Dec 13, 2005
Loading...

Share This Page