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


Groups > comp.lang.python > #30935

Re: Unpaking Tuple

From Terry Reedy <tjreedy@udel.edu>
Subject Re: Unpaking Tuple
Date 2012-10-07 16:03 -0400
References <801f0e2c-7d1d-4e91-bec5-78c5e53a70ec@googlegroups.com> <69987244-3aa8-441b-83e8-2275e75f2b83@ph9g2000pbb.googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.1934.1349640228.27098.python-list@python.org> (permalink)

Show all headers | View raw


On 10/7/2012 1:58 PM, woooee wrote:
> On Oct 6, 3:09 am, sajuptpm <sajup...@gmail.com> 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

Back to comp.lang.python | Previous | NextPrevious 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