Splitting a tuple to use values ​​as a filter in a sql query

I had a large list of ids and I would like to use it as a filter in a SQL query to query the database in the format Select * from Table where ID in MyList.

Unfortunately my list is about 800 records and is rejected for too long. I need an easy way to split it into multiple queries using smaller lists.

For my code, I have made a list of just a few entries and they represent IDs. I split it successfully, but it faces multiple issues. How can I contact them?

  • It seems very inefficient and non-pythonic
  • If I need to think ahead of time about how many different tuples to split. If there are 10,000 records, I cannot just split it in half, as each part will still be too complex.

My code:

mylist = (12,32,3,45,34,56,45)

mylist1 = []
mylist2 = []
for i in range(0, len(mylist)/2):
    mylist1.append(mylist[i])
for i in range(len(mylist)/2+1, len(mylist)):
    mylist2.append(mylist[i])


print mylist1
print mylist2

      

+3


source to share


1 answer


The while loop will let you use the list in the sizes you like, for example.



data = range(10)

chunk_size = 10

while data:
    sublist, data = data[:chunk_size], data[chunk_size:]

    # Handle subplit
    print sublist

      

+1


source







All Articles