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

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

From
"W. Matthew Wilson"
Date:
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

On Sun, Jun 17, 2012 at 4:55 PM, W. Matthew Wilson <matt@tplus1.com> wrote:

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

No, there's nothing like this. For me, any attempt to add a feature
and let the user choose whether to return one-column queries just as
lists or as one-item tuples (a cursor subclass? a function?) seems
more verbose than the [r[0] for r in cur] or map(itemgetter(0), cur)
to get the data the way you want.

-- Daniele

On Sun, Jun 17, 2012 at 12:32 PM, Daniele Varrazzo
<daniele.varrazzo@gmail.com> wrote:

> No, there's nothing like this. For me, any attempt to add a feature
> and let the user choose whether to return one-column queries just as
> lists or as one-item tuples (a cursor subclass? a function?) seems
> more verbose than the [r[0] for r in cur] or map(itemgetter(0), cur)
> to get the data the way you want.

Thanks for the reply!  Mostly I wanted to make sure there wasn't
already a standard solution out there.

For me, it just feels goofy to write something like [row[0] for row in
cursor.fetchall()], but I understand where you are coming from.

I like the cursor subclass idea so maybe I'll take a shot at something
like that.

Matt

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

You could always make use of tuple-unpacking and try something like

[element for (element,) in cursor.fetchall()]

It feels slightly more pythonic to me, though not by much.

Thanks,
Nick Cash

-----Original Message-----
From: psycopg-owner@postgresql.org [mailto:psycopg-owner@postgresql.org] On Behalf Of W. Matthew Wilson
Sent: Tuesday, June 19, 2012 13:36
To: Daniele Varrazzo
Cc: psycopg@postgresql.org
Subject: Re: [psycopg] When I select a single column, can I prevent getting a list of one-element tuples?

On Sun, Jun 17, 2012 at 12:32 PM, Daniele Varrazzo <daniele.varrazzo@gmail.com> wrote:

> No, there's nothing like this. For me, any attempt to add a feature
> and let the user choose whether to return one-column queries just as
> lists or as one-item tuples (a cursor subclass? a function?) seems
> more verbose than the [r[0] for r in cur] or map(itemgetter(0), cur)
> to get the data the way you want.

Thanks for the reply!  Mostly I wanted to make sure there wasn't already a standard solution out there.

For me, it just feels goofy to write something like [row[0] for row in cursor.fetchall()], but I understand where you
arecoming from. 

I like the cursor subclass idea so maybe I'll take a shot at something like that.

Matt

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

--
Sent via psycopg mailing list (psycopg@postgresql.org) To make changes to your subscription:
http://www.postgresql.org/mailpref/psycopg