Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8418159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:14:40+00:00 2026-06-10T02:14:40+00:00

I would like to know how to pass a list/set/tuple from python (via psycopg2)

  • 0

I would like to know how to pass a list/set/tuple from python (via psycopg2) to a postgres query as a one-column table. For example, if the list is ['Alice', 'Bob'], I want the table to be:

| Temp  |
+-------+
| Alice |
| Bob   |

If anybody has alternate suggestions to achieve my result after reading the section below, that would be fine as well.

Background

I have an SQL table which has three columns of interest:

ID | Members | Group
---+---------+----------
1  | Alice   | 1
2  | Alice   | 1
3  | Bob     | 1
4  | Charlie | 1
5  | Alice   | 2
6  | Bob     | 2
7  | Alice   | 3
8  | Bob     | 4
9  | Charlie | 3

I want a table of groups with certain combinations of members. Note that a member may have multiple items in a group (e.g. IDs 1 and 2).

For an input of ['Alice'] I would want which groups she is in (present) and which contain only her (unique), as below:

Group | Type
------+--------
1     | present
2     | present
3     | present

For an input of ['Alice', 'Bob']:

Group | Type
------+--------
1     | present
2     | unique

From reading it looks like I am looking for relational division as described here, for which I need to do what the original question asks as the input is taken from a web form processed in python. Again, alternative solutions are also welcome.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T02:14:42+00:00Added an answer on June 10, 2026 at 2:14 am

    You need to make a subquery where you create member counts, then do a simple divisor query with a GROUP BY statement, but against a IN static_set clause instead of against another table. Because this is python, you already know the size of the static set.

    I’ll assume you already have a database cursor, and the table is called GroupMembers:

    MEMBERSHIP_QUERY = '''
        SELECT gm.group, mc.memberscount = %(len)s AS type
          FROM groupmembers gm
          JOIN (SELECT "group", COUNT(DISTINCT members) as memberscount 
                  FROM groupmembers
                 GROUP BY "group") mc
          ON gm.group = mc.group
        WHERE gm.members IN %(set)s
        GROUP BY gm.group, mc.memberscount
        HAVING COUNT(DISTINCT gm.members) = %(len)s;
    '''
    
    def membership(members):
        # obtain a cursor
        for row in cursor.execute(MEMBERSHIP_QUERY, dict(len=len(members), set=members)):
            yield dict(group=row[0], type=row[1])
    

    There is thus no need to use a TEMP table to execute this query.

    If you do need a TEMP table for other purposes, inserting a set of rows is easiest with .executemany():

    members = ['Alice', 'Bob']
    cursor.execute('CREATE TEMP TABLE tmp_members (member CHAR(255)) ON COMMIT DROP;')
    cursor.executemany('INSERT INTO tmp_members VALUES (%s);',
        [(name,) for name in members])
    

    Note that .executemany() expects a sequence of sequences; each entry is a sequence of row data, which in this case only holds one name each. I generate a list of single-item tuples to fill the table.

    Alternatively, you can use a sequence of mappings too and use the %(name)s parameter syntax (so the row data sequence becomes [dict(name=name) for name in members]).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know how I can pass a list with values in
I would like to know how to pass single quotations as an argument in
I would like to know how browser allow viruses to pass through to our
I would like to know if it's possible to pass a parameter to a
I would like to know how can I pass a ruby variable inside an
I would like to know how I can do a low-pass filter in opencv
Would like to know how to hide an div after a set of css3
i would like know some reference. I know i can googling it. but prefer
Would like to know what a programmer should know to become a good at
Would like to know the c# code to actually retrieve the IP type: Static

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.