tuple assign variables

Simon Brunning simon.brunning at gmail.com
Mon Aug 15 13:40:10 CEST 2005

On 8/13/05, dimitri pater <dimitri.pater at gmail.com> wrote:
>  selecting a record from a MySQL database results in a tuple like this:
>  (('Evelyn', 'Dog', 'No'),)
>  I want to assign every item of that tuple to a variable using this code
> (deep_list is taken from the Python Cookbook):
>  def deep_list(x):
>      if type(x)!=type( () ):
>          return x
>      return map(deep_list,x)
>  mytuple = (('Evelyn', 'Dog', 'No'),)
>  mylist = deep_list(mytuple)
>  for item in mylist:
>    name, pet, modified = item
>  it woks fine, but I wonder if there is a better way to achieve this.
>  Any suggestions?

I'm fond of Greg Stein's dtuple module[1]. I've never used it with
MySQL, so YMMV, but using it might look something like:

descriptor = dtuple.TupleDescriptor(cursor.description)

for row in (dtuple.DatabaseTuple(descriptor, row) for row in cursor.fetchall()):
    print row.name # Prints "Evelyn"
    # etc...

If your result set will be large, see also ResultIter[2].

Simon B,
simon at brunningonline.net,

[1] http://www.lyra.org/greg/python/dtuple.py
[2] http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/137270

More information about the Python-list mailing list