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


Groups > comp.lang.python > #17481

Re: Newbie Question: Obtain element from list of tuples

From Roy Smith <roy@panix.com>
Newsgroups comp.lang.python
Subject Re: Newbie Question: Obtain element from list of tuples
Date 2011-12-18 16:10 -0500
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <roy-78470D.16102518122011@news.panix.com> (permalink)
References <jclflh$u45$1@news.albasani.net> <mailman.3807.1324241477.27778.python-list@python.org>

Show all headers | View raw


In article <mailman.3807.1324241477.27778.python-list@python.org>,
 Chris Angelico <rosuav@gmail.com> wrote:

> 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]

Although, if you're going to do that, you might as well take advantage 
of the fetchone() method that most Python database APIs have and 
eliminate one of the indexes.  Typical code would be something like:

cursor.execute("SELECT mode FROM config WHERE id=5")
mode = cursor.fetchone()[0]

If you're going to be doing any database work in Python, you should be 
familiar with the Python Database API Specification, 
http://www.python.org/dev/peps/pep-0249/.  Most of the vendor-specific 
database modules have interfaces which hold pretty close to the style 
described there.

You might also want to explore SQL_Alchemy.  I personally find it 
difficult and painful to work with, but it does have the powerful 
advantage that it abstracts away all the vendor-specific differences 
between databases.

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