Received: by 10.68.232.1 with SMTP id tk1mr73795pbc.7.1349937111667; Wed, 10 Oct 2012 23:31:51 -0700 (PDT) Path: s9ni838pbb.0!nntp.google.com!border1.nntp.dca.giganews.com!border4.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newspeer1.nac.net!news.mi.ras.ru!aspen.stu.neva.ru!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-l...@python.org Delivered-To: python-l...@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'variables.': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'terry': 0.09; 'tuple': 0.09; 'unpack': 0.09; 'unpacking': 0.09; 'valueerror': 0.09; 'buggy': 0.16; 'oct': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'wrote:': 0.17; 'instance,': 0.17; 'jan': 0.18; '>>>': 0.18; 'produces': 0.22; 'example': 0.23; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; 'am,': 0.27; 'necessary.': 0.27; 'header:X -Complaints-To:1': 0.28; 'code': 0.31; 'print': 0.32; 'to:addr :python-list': 0.33; 'pm,': 0.35; 'received:org': 0.36; 'except': 0.36; 'skip:{ 10': 0.36; 'why': 0.37; 'item': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'below.': 0.68; "'make'": 0.84; 'received:fios.verizon.net': 0.84; 'seldom': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-l...@python.org From: Terry Reedy Subject: Re: Unpaking Tuple Date: Sun, 07 Oct 2012 16:03:20 -0400 References: <801f0e2c-7d1d-4e91-bec5-78c5e53a70ec@googlegroups.com> <69987244-3aa8-441b-83e8-2275e75f2b83@ph9g2000pbb.googlegroups.com> Mime-Version: 1.0 X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20120824 Thunderbird/15.0 In-Reply-To: <69987244-3aa8-441b-83e8-2275e75f2...@ph9g2000pbb.googlegroups.com> X-BeenThere: python-l...@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1349640228 news.xs4all.nl 6966 [2001:888:2000:d::a6]:42031 X-Complaints-To: ab...@xs4all.nl Bytes: 3833 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 10/7/2012 1:58 PM, woooee wrote: > On Oct 6, 3:09 am, sajuptpm wrote: >> 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) You cannot 'make' buggy code work -- except by changing it. >>> a, b, c, *d = (1,2,3) >>> d [] > Why is it necessary to unpack the tuple into arbitrary variables. It is not necessary. > a_tuple=(1,2,3) > for v in a_tuple: > print v This is often the right way to access any iterable. > for ctr in range(len(a_tuple)): > print a_tuple[ctr] This is seldom the right way. See the example below. Unpacking is for when you have known-length iterables. For instance, enumerate produces pairs. >>> for i, v in enumerate('abc'): print('Item {} is {}.'.format(i, v)) Item 0 is a. Item 1 is b. Item 2 is c. -- Terry Jan Reedy