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 7400549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:15:56+00:00 2026-05-29T04:15:56+00:00

Having a list of 3-tuples : [(a, b, c), (d, e, f)] I want

  • 0

Having a list of 3-tuples :

[(a, b, c), (d, e, f)]

I want to retrieve all the rows from a table where 3 columns matches the tuples. FOr this example, the query WHERE clause could be something like this :

   (column_X = a AND column_Y = b AND column_Z = c)
OR (column_X = d AND column_Y = e AND column_Z = f)

How can I create such a request using SQLAlchemy ? In my case the 3-tuples list will contains hundred of elements, and I’m looking for the best scallable solution.

Thanks for your help,

  • 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-05-29T04:15:57+00:00Added an answer on May 29, 2026 at 4:15 am

    Easiest way would be using SQLAlchemy-provided tuple_ function:

    from sqlalchemy import tuple_
    
    session.query(Foo).filter(tuple_(Foo.a, Foo.b, Foo.c).in_(items))
    

    This works with PostgreSQL, but breaks with SQLite. Not sure about other database engines.

    Fortunately there’s a workaround that should work on all databases.

    Start by mapping out all the items with the and_ expression:

    conditions = (and_(c1=x, c2=y, c3=z) for (x, y, z) in items)
    

    And then create an or_ filter that encloses all the conditions:

    q.filter(or_(*conditions))
    

    Here’s a simple example:

    #/usr/bin/env python
    from sqlalchemy import create_engine
    from sqlalchemy import Column, Integer
    from sqlalchemy.sql import and_, or_
    from sqlalchemy.orm import sessionmaker
    from sqlalchemy.ext.declarative import declarative_base
    
    engine = create_engine('sqlite:///')
    session = sessionmaker(bind=engine)()
    Base = declarative_base()
    
    class Foo(Base):
        __tablename__ = 'foo'
    
        id = Column(Integer, primary_key=True)
        a = Column(Integer)
        b = Column(Integer)
        c = Column(Integer)
    
        def __init__(self, a, b, c):
            self.a = a
            self.b = b
            self.c = c
    
        def __repr__(self):
            return '(%d %d %d)' % (self.a, self.b, self.c)
    
    Base.metadata.create_all(engine)
    
    session.add_all([Foo(1, 2, 3), Foo(3, 2, 1), Foo(3, 3, 3), Foo(1, 3, 4)])
    session.commit()
    items = ((1, 2, 3), (3, 3, 3))
    conditions = (and_(Foo.a==x, Foo.b==y, Foo.c==z) for (x, y, z) in items)
    q = session.query(Foo)
    print q.all()
    q = q.filter(or_(*conditions))
    print q
    print q.all()
    

    Which outputs:

    $ python test.py 
    [(1 2 3), (3 2 1), (3 3 3), (1 3 4)]
    SELECT foo.id AS foo_id, foo.a AS foo_a, foo.b AS foo_b, foo.c AS foo_c 
    FROM foo 
    WHERE foo.a = :a_1 AND foo.b = :b_1 AND foo.c = :c_1 OR foo.a = :a_2 AND foo.b = :b_2 AND foo.c = :c_2
    [(1 2 3), (3 3 3)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having list of char array by that I want to store all
I am having two tuples in a list. For example: [(A,100,2),(B,50,3)] . I need
I want to sort a list of named tuples without having to remember the
I have this table: SUBSCRIPTION NAME SUBSCRIBER It's essentially a table having a list
Im having a list of student names in a table , in the same
Here i am having a unordered list displaying two columns code and name .I
I have some troubles with a method having a typed List parameter, inherited from
I'm having a hard time defining this question. I want to do something like
So I have this list containing tuples, and I have also written a code
Having some trouble with this code. Trying to return a tuple of tuples (coordinates)

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.