Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Unpaking Tuple
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Chris Rebert  
View profile  
 More options Oct 6 2012, 6:27 am
Newsgroups: comp.lang.python
From: Chris Rebert <c...@rebertia.com>
Date: Sat, 6 Oct 2012 03:27:52 -0700
Local: Sat, Oct 6 2012 6:27 am
Subject: Re: Unpaking Tuple

On Sat, Oct 6, 2012 at 3:09 AM, sajuptpm <sajup...@gmail.com> wrote:
> Hi,

> I am using python 2.6.

> I need a way to make following code working without any ValueError .
>>>> a, b, c, d = (1,2,3,4)
>>>> a, b, c, d = (1,2,3).

> Note: Number of values in the tuple will change dynamically.

Then you arguably want a list, not a tuple.

But at any rate:
shortfall = 4 - len(your_tuple)
your_tuple += (None,) * shortfall # assuming None is a suitable default
a, b, c, d = your_tuple

If you also need to handle the "too many items" case, use slicing:
a, b, c, d = your_tuple[:4]

Cheers,
Chris


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.