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


Groups > comp.lang.python > #17500

Re: Newbie Question: Obtain element from list of tuples

From "Frank Millman" <frank@chagford.com>
Subject Re: Newbie Question: Obtain element from list of tuples
Date 2011-12-19 08:46 +0200
References <jclflh$u45$1@news.albasani.net><roy-B739A1.15041318122011@news.panix.com><2c286dea-efba-45d5-85a7-a7503733575a@i6g2000vbe.googlegroups.com> <4eeea8eb$0$11091$c3e8da3@news.astraweb.com>
Newsgroups comp.lang.python
Message-ID <mailman.3815.1324277206.27778.python-list@python.org> (permalink)

Show all headers | View raw


"Steven D'Aprano" <steve+comp.lang.python@pearwood.info> wrote in message 
news:4eeea8eb$0$11091$c3e8da3@news.astraweb.com...
> On Sun, 18 Dec 2011 18:35:47 -0800, alex23 wrote:
>
>> Pre-namedtuple, I used to like using named slices for this:
>>
>>     cPID = slice(19)
>>     pid = recs[cPID]
>
> You know, that is an incredibly simple thing and yet it never dawned on
> me before now. Thank you for sharing that.
>

I also like it, but it does not work quite so simply for me.

>>> row = tuple(range(20))
>>> cPID = slice(15)
>>> pid = row[cPID]
>>> pid
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)
>>>

This works -

>>> row = tuple(range(20))
>>> cPID = slice(15, 16)
>>> pid, = row[cPID]  # note the trailing comma
>>> pid
15
>>>

Still nice, but not quite so elegant.

Am I missing something?

Frank Millman

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


Thread

Newbie Question: Obtain element from list of tuples HoneyMonster <someone@someplace.invalid> - 2011-12-18 19:41 +0000
  Re: Newbie Question: Obtain element from list of tuples Arnaud Delobelle <arnodel@gmail.com> - 2011-12-18 19:49 +0000
  Re: Newbie Question: Obtain element from list of tuples Roy Smith <roy@panix.com> - 2011-12-18 15:04 -0500
    Re: Newbie Question: Obtain element from list of tuples HoneyMonster <someone@someplace.invalid> - 2011-12-18 20:22 +0000
      Re: Newbie Question: Obtain element from list of tuples Roy Smith <roy@panix.com> - 2011-12-18 15:25 -0500
    Re: Newbie Question: Obtain element from list of tuples alex23 <wuwei23@gmail.com> - 2011-12-18 18:35 -0800
      Re: Newbie Question: Obtain element from list of tuples Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-19 03:00 +0000
        Re: Newbie Question: Obtain element from list of tuples "Frank Millman" <frank@chagford.com> - 2011-12-19 08:46 +0200
          Re: Newbie Question: Obtain element from list of tuples DevPlayer <devplayer@gmail.com> - 2011-12-19 07:37 -0800
          Re: Newbie Question: Obtain element from list of tuples alex23 <wuwei23@gmail.com> - 2011-12-19 19:53 -0800
  Re: Newbie Question: Obtain element from list of tuples Chris Angelico <rosuav@gmail.com> - 2011-12-19 07:51 +1100
    Re: Newbie Question: Obtain element from list of tuples Roy Smith <roy@panix.com> - 2011-12-18 16:10 -0500
    Re: Newbie Question: Obtain element from list of tuples HoneyMonster <someone@someplace.invalid> - 2011-12-18 22:55 +0000
      Re: Newbie Question: Obtain element from list of tuples Chris Angelico <rosuav@gmail.com> - 2011-12-19 10:40 +1100
        Re: Newbie Question: Obtain element from list of tuples HoneyMonster <someone@someplace.invalid> - 2011-12-20 00:59 +0000

csiph-web