Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #30883

Re: Unpaking Tuple

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <chris@rebertia.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.006
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'python': 0.09; 'received :mail-vb0-f46.google.com': 0.09; 'tuple': 0.09; 'tuple.': 0.09; 'valueerror': 0.09; 'cc:addr:python-list': 0.10; 'sat,': 0.15; 'oct': 0.16; 'slicing:': 0.16; 'wrote:': 0.17; 'received:209.85.212.46': 0.18; 'assuming': 0.22; 'cheers,': 0.23; 'cc:2**0': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'values': 0.26; 'am,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'received:209.85.212': 0.28; 'chris': 0.28; '>>>>': 0.29; 'items"': 0.29; 'case,': 0.29; 'code': 0.31; 'handle': 0.33; 'hi,': 0.33; 'received:google.com': 0.34; 'received:209.85': 0.35; 'but': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'list,': 0.39; 'header:Received:5': 0.40; 'note:': 0.64; '"too': 0.84; 'sender:addr:chris': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type; bh=YaFoGpj1PnMPh2YomhcgcxU3XlC1prCuG068dBlestI=; b=SIS22SK866SMXDd6OfQwYmDDKey5mjcwQjLVOAjU8dQZSRRVqYdFdi3OuJ3G4/eGZG FaEwIiAE1kLz9xO0ZZwGWE4gqSST0ttUL67JughQx/0MLqZvD42AaT2Q6+hblzvfuLTg EI/GZMbTOkv3aCB0I73CcPQYovkFB26cnaw/c=
X-Google-DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :x-gm-message-state; bh=YaFoGpj1PnMPh2YomhcgcxU3XlC1prCuG068dBlestI=; b=IG9azsPULu43a8fwBrmG1p+ANaqNGDr2Bu3toijrhHKa6aXDfjSsXxhfOubq2f1ixc LIhoXllSyyQLWBKcKlrt4f7UTNsku+q1MKXvgvML2M8m9HP58YmZZi8b3RwBGePQ7rRV ZjScR8DpRbBIl7cMh6xK2rbi5tB7qlpSzHyzBeVmR1OmEJlZ5zaulmWqyKRufdtt+RK5 rz5lMDUp18yAOW+mlaB9mG70yLSsyAbBxuBcJqMMd7/Ep1Jk2D6xoB+1r98oPHu2x1kP k4zfRXl9y8Z7ELa4YGY0sU+YU9avkhOcEMtozManB1FJFAk6IvDOm4/F25JygTKYx4XZ 9/1A==
MIME-Version 1.0
Sender chris@rebertia.com
In-Reply-To <801f0e2c-7d1d-4e91-bec5-78c5e53a70ec@googlegroups.com>
References <801f0e2c-7d1d-4e91-bec5-78c5e53a70ec@googlegroups.com>
Date Sat, 6 Oct 2012 03:27:52 -0700
X-Google-Sender-Auth HIG_om7rafsLT1THawxxj8Iv4rs
Subject Re: Unpaking Tuple
From Chris Rebert <clp2@rebertia.com>
To sajuptpm <sajuptpm@gmail.com>
Content-Type text/plain; charset=UTF-8
X-Gm-Message-State ALoCoQmrrKkz10czrhFsNaoezgQDKBtYM6/yGvhHpl7M848Zl+HWEkW1kUBUViQC4hbfE6eT5Yg4
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1898.1349519275.27098.python-list@python.org> (permalink)
Lines 23
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1349519275 news.xs4all.nl 6874 [2001:888:2000:d::a6]:33705
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:30883

Show key headers only | View raw


On Sat, Oct 6, 2012 at 3:09 AM, sajuptpm <sajuptpm@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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Unpaking Tuple sajuptpm <sajuptpm@gmail.com> - 2012-10-06 03:09 -0700
  Re: Unpaking Tuple Chris Rebert <clp2@rebertia.com> - 2012-10-06 03:27 -0700
    Re: Unpaking Tuple Roy Smith <roy@panix.com> - 2012-10-06 08:46 -0400
      Re: Unpaking Tuple Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-06 15:08 +0000
        Re: Unpaking Tuple Thomas Bach <thbach@students.uni-mainz.de> - 2012-10-08 23:45 +0200
        RE: Unpaking Tuple "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-10-08 22:21 +0000
          Re: RE: Unpaking Tuple Bob Martin <bob.martin@excite.com> - 2012-10-09 07:07 +0100
            Re: Unpaking Tuple Dave Angel <d@davea.name> - 2012-10-09 02:29 -0400
              Re: Unpaking Tuple Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2012-10-09 10:22 +0300
                Re: mangled messages (was: Unpaking Tuple) Tim Chase <python.list@tim.thechases.com> - 2012-10-09 05:48 -0500
                Re: mangled messages (was: Unpaking Tuple) Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2012-10-09 15:05 +0300
                Re: mangled messages Tim Chase <python.list@tim.thechases.com> - 2012-10-09 09:26 -0500
            Re: Unpaking Tuple Grant Edwards <invalid@invalid.invalid> - 2012-10-09 14:11 +0000
            RE: RE: Unpaking Tuple "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-10-09 16:40 +0000
            Re: Unpaking Tuple Robert Miles <robertmiles@teranews.com> - 2012-11-18 19:14 -0600
            Re: Unpaking Tuple Hans Mulder <hansmu@xs4all.nl> - 2012-11-19 02:56 +0100
  Re: Unpaking Tuple woooee <woooee@gmail.com> - 2012-10-07 10:58 -0700
    Re: Unpaking Tuple Terry Reedy <tjreedy@udel.edu> - 2012-10-07 16:03 -0400

csiph-web