Unpack less values from function's return values

Bobby bobby.house at gmail.com
Thu May 28 10:47:32 EDT 2009


On May 28, 5:40 am, Chris Rebert <c... at rebertia.com> wrote:
> On Thu, May 28, 2009 at 3:19 AM,  <dudeja.ra... at gmail.com> 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]



More information about the Python-list mailing list