Newbie Question: Obtain element from list of tuples

Arnaud Delobelle arnodel at gmail.com
Sun Dec 18 14:49:34 EST 2011


On 18 December 2011 19:41, HoneyMonster <someone at someplace.invalid> wrote:
> Hi,
>
> I'm just starting to learn Python, so please bear with me. I have in my
> program an object (recs) which is a list of tuples (returned as such by a
> database query).
>
> My question is doubtless a very easy one to answer: Say I want the ninth
> element in the twentieth tuple put into variable PID, I can do this,
> bearing in mind that numbering starts at zero:
>
> tup = recs[19]
> PID = tup[8]
>
> But there must be an easier way; i.e. to do it in one go without the
> extra variable. How do I achieve that please?

Well, you were almost there:

pid = recs[19][8]

Note: all caps is usually reserved for constants in Python.

-- 
Arnaud



More information about the Python-list mailing list