When I select a single column, can I prevent getting a list of one-element tuples? - Mailing list psycopg

From W. Matthew Wilson
Subject When I select a single column, can I prevent getting a list of one-element tuples?
Date
Msg-id CAGHfCUA8U6_VvdsmfafA7EHqrU68L+2Xz-eKyJpnMBqESW6aHg@mail.gmail.com
Whole thread Raw
Responses Re: When I select a single column, can I prevent getting a list of one-element tuples?
List psycopg
I have some code that looks sort of like this::

    cursor.execute("""
        select user_email_address
        from my_user_table
        """)

   my_users = cursor.fetchall()

and my_users is a list of one-element lists.

Now I have to index into that inner list on every row when I want to
print my results like this::

    for row in my_users:
        print row[0]

 Or if I use the excellent dictcursor from the extras package I can do
this instead::

    for row in my_users:
        print row['user_email_address']

This is not a huge problem, but it irritates me because I often forget
about it and then get an error and have to go back and fix it.

I understand that when I ask for more than one column in a query,
psycopg2 MUST return each row as a list.  But when I am asking for
exactly one column, is there a way to tell psycopg2 to not return a
list for every row, but just the single value?

Thanks for the help.

Matt

--
W. Matthew Wilson
matt@tplus1.com
http://tplus1.com

psycopg by date:

Previous
From: Bill House
Date:
Subject: Capacity questions
Next
From: Daniele Varrazzo
Date:
Subject: Re: When I select a single column, can I prevent getting a list of one-element tuples?