Unpack less values from function's return values

MRAB google at mrabarnett.plus.com
Thu May 28 09:46:34 EDT 2009


Chris Rebert wrote:
> On Thu, May 28, 2009 at 3:19 AM,  <dudeja.rajat 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()
> 
Or:

a, b = foo()[ : 2]



More information about the Python-list mailing list