unpack tuple of wrong size

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tung Wai Yip

    unpack tuple of wrong size

    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
  • Andres Rosado-Sepulveda

    #2
    Re: unpack tuple of wrong size

    Tung Wai Yip wrote:
    [color=blue]
    > 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?[/color]

    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: andresr@despamm ed.com
    Homepage: http://andres980.tripod.com/

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

    Comment

    • Duncan Booth

      #3
      Re: unpack tuple of wrong size

      Tung Wai Yip <tungwaiyip@yah oo.com> wrote in
      news:c7r57095as sq7p777q2ugudnh 72qud8kgb@4ax.c om:
      [color=blue]
      > 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?[/color]

      Probably the simplest is:

      a, b = (t + (None, None))[:2]

      Comment

      Working...