On Mon, 19 Dec 2011 07:51:08 +1100, Chris Angelico wrote: > On Mon, Dec 19, 2011 at 6:41 AM, HoneyMonster > <someone@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!
Thanks, Chris. Actually I had simplified the question somewhat. The actuality is indeed - as you suspected - that recs = conn.query("SELECT * FROM some_table") but I am then using the recs object to populate a (non editable) wxGrid. When the user selects a row and clicks a button, I am using: pos = self.grid_1.GetGridCursorRow() to establish which tuple in recs is involved, and then pid = recs[pos][4] to determine the key value (I suspected that accessing recs directly would be more efficient that trying to determine the wxGrid column value, and in any case the pid is in recs but not in the wxGrid). I trust this all makes sense. Thanks again. -- http://mail.python.org/mailman/listinfo/python-list