Newbie Question: Obtain element from list of tuples

Frank Millman frank at chagford.com
Mon Dec 19 01:46:28 EST 2011


"Steven D'Aprano" <steve+comp.lang.python at pearwood.info> wrote in message 
news:4eeea8eb$0$11091$c3e8da3 at 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





More information about the Python-list mailing list