Newbie Question: Obtain element from list of tuples
Chris Angelico
rosuav at gmail.com
Sun Dec 18 15:51:08 EST 2011
More information about the Python-list mailing list
Sun Dec 18 15:51:08 EST 2011
- Previous message (by thread): Newbie Question: Obtain element from list of tuples
- Next message (by thread): Newbie Question: Obtain element from list of tuples
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Mon, Dec 19, 2011 at 6:41 AM, HoneyMonster <someone at someplace.invalid> wrote: > 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? The specific answer has already been given, but I'd like to fill in the generality. In Python, everything's an object; if you can do something with a variable, you can do it with an expression too. I don't know what your function call is to obtain your record list, but imagine it's: recs = conn.query("SELECT * FROM some_table") Then you can actually do all of this in a single statement. It's not usually what you'll want to do, but this is legal: pid = conn.query("SELECT * FROM some_table")[19][8] If you're absolutely certain that you'll always get precisely one value from a query, this becomes rather more useful: mode = conn.query("SELECT mode FROM config WHERE id=5")[0][0] In any case: You can work with a function's return value directly, without first storing it in a temporary variable. Hope that helps! Chris Angelico
- Previous message (by thread): Newbie Question: Obtain element from list of tuples
- Next message (by thread): Newbie Question: Obtain element from list of tuples
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list