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


Groups > comp.lang.python > #17476

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 15:04 -0500
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <roy-B739A1.15041318122011@news.panix.com> (permalink)
References <jclflh$u45$1@news.albasani.net>

Show all headers | View raw


In article <jclflh$u45$1@news.albasani.net>,
 HoneyMonster <someone@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).

Sounds like a standard database interface -- each tuple represents one 
row in the query result.  Very common.

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

Sure, you could do:

PID = recs[19][8]

That's shorter, but it probably not better.  Presumably, the tuple 
elements represent columns in the database.  It would make the code 
easier to read if you unpacked the tuple into something with 
human-readable names:

col_1, col_2, col_3, ...... col_n = recs[19]

and then use the name for the column of interest.  A common convention 
is that when you're unpacking a tuple (or a list, I suppose) and are 
only interested in some of the elements, you unpack the others into "_".  
Thus:

_, _, _, _, pid, _, _, _ = recs[19]

Of course, if you're going to do all that, you probably could have 
written a better query which returned just the column you were 
interested in.  Instead of "select * from foo", you could have done 
"select pid from foo" (assuming you're using some SQL-based database).

> Secondly, I am more than happy to read the fine manuals. If someone could 
> point me in the direction of one or two "for idiots" guides, that would 
> be greatly appreciated.

The on-line tutorial is pretty good.  
http://docs.python.org/tutorial/index.html

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