Re: Unpack less values from function's return values

Discussion in 'Python' started by Chris Rebert, May 28, 2009.

  1. Chris Rebert

    Chris Rebert Guest

    On Thu, May 28, 2009 at 3:19 AM, <> wrote:
    > Hi,
    >
    > I'm using Python 2.5.2. I'm getting this error whenever I try to unpack less
    > values from a function.
    >
    > ValueError: too many values to unpack
    >
    >
    > I want to know if there is a way I can unpack less values returning from a
    > function?


    Unpack them into throwaway variables:

    def foo(): return 1,2,3,4

    a, b, _, _ = foo()

    In very new Python, you can also do:

    a, b, *_ = foo()

    Cheers,
    Chris
    --
    http://blog.rebertia.com
     
    Chris Rebert, May 28, 2009
    #1
    1. Advertising

  2. Chris Rebert

    Bobby Guest

    On May 28, 5:40 am, Chris Rebert <> wrote:
    > On Thu, May 28, 2009 at 3:19 AM,  <> wrote:
    > > Hi,

    >
    > > I'm using Python 2.5.2. I'm getting this error whenever I try to unpack less
    > > values from a function.

    >
    > > ValueError: too many values to unpack

    >
    > > I want to know if there is a way I can unpack less values returning from a
    > > function?

    >
    > Unpack them into throwaway variables:
    >
    > def foo(): return 1,2,3,4
    >
    > a, b, _, _ = foo()
    >
    > In very new Python, you can also do:
    >
    > a, b, *_ = foo()
    >
    > Cheers,
    > Chris
    > --http://blog.rebertia.com


    You could also do something like
    a,b = foo()[:2]
     
    Bobby, May 28, 2009
    #2
    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. Elezar Simeon Papo

    ValueError: need more than 3 values to unpack

    Elezar Simeon Papo, Jan 21, 2006, in forum: Python
    Replies:
    3
    Views:
    1,446
    Elezar Simeon Papo
    Feb 7, 2006
  2. cprogrammer

    struct.unpack less than 1 byte

    cprogrammer, Oct 10, 2007, in forum: Python
    Replies:
    4
    Views:
    306
    Stargaming
    Oct 10, 2007
  3. jiajia wu
    Replies:
    0
    Views:
    380
    jiajia wu
    Oct 1, 2009
  4. 6668
    Replies:
    0
    Views:
    174
  5. lllll
    Replies:
    0
    Views:
    162
    lllll
    Jun 8, 2009
Loading...

Share This Page