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


Groups > comp.lang.python > #17480

Re: Newbie Question: Obtain element from list of tuples

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python,': 0.01; 'variable,': 0.07; 'given,': 0.09; 'object;': 0.09; 'query,': 0.09; 'statement.': 0.09; 'storing': 0.09; 'tuple': 0.09; 'way;': 0.09; 'am,': 0.12; 'received:209.85.210.174': 0.13; 'received :mail-iy0-f174.google.com': 0.13; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; "function's": 0.16; 'helps!': 0.16; 'numbering': 0.16; 'pid,': 0.16; 'subject:Newbie': 0.16; 'variable.': 0.16; 'zero:': 0.16; 'mon,': 0.16; 'wrote:': 0.18; 'please?': 0.18; 'subject:list': 0.21; 'subject:Question': 0.21; 'dec': 0.22; 'header:In-Reply-To:1': 0.22; 'starts': 0.24; 'do,': 0.25; 'mode': 0.25; 'function': 0.27; 'variable': 0.28; 'message- id:@mail.gmail.com': 0.28; 'temporary': 0.29; 'config': 0.30; 'chris': 0.30; 'usually': 0.31; 'actually': 0.33; 'rather': 0.33; 'there': 0.33; 'to:addr:python-list': 0.34; 'certain': 0.34; 'record': 0.35; 'something': 0.35; 'question': 0.36; 'element': 0.37; 'but': 0.37; 'list,': 0.37; 'received:google.com': 0.37; 'received:209.85': 0.38; 'put': 0.38; 'subject:from': 0.38; 'easier': 0.38; 'i.e.': 0.39; "i'd": 0.39; "it's": 0.40; 'received:209': 0.40; 'to:addr:python.org': 0.40; 'hope': 0.61; 'more': 0.61; '2011': 0.61; 'achieve': 0.61; 'your': 0.61; '19,': 0.68; 'imagine': 0.71; "everything's": 0.91; 'ninth': 0.91; 'absolutely': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=6Ki5w8KENfKgv55sNvybVC7TWihAzSSbuEuiKGXEWhY=; b=Pp2y7HDpnXaRiddU1rAWipSN3udJ5rRAY/PjktaV/BuNUwU1VLfKD07+jho3fFw7DL SWPtzDj5rJlB6Gpsh2XBG/TwCG6nOn/lwBksqWTmitD8UABxcq0bYsA90XxkztEy77qe ETDHC9UVQVOFl9e38amU7bRdGJqgBgJ4g2cK4=
MIME-Version 1.0
In-Reply-To <jclflh$u45$1@news.albasani.net>
References <jclflh$u45$1@news.albasani.net>
Date Mon, 19 Dec 2011 07:51:08 +1100
Subject Re: Newbie Question: Obtain element from list of tuples
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3807.1324241477.27778.python-list@python.org> (permalink)
Lines 35
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1324241477 news.xs4all.nl 6917 [2001:888:2000:d::a6]:46803
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:17480

Show key headers only | View raw


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!

Chris Angelico

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