Python list.remove items present in second list -


i've searched around , of errors see when people trying iterate on list , modify @ same time. in case, trying take 1 list, , remove items list present in second list.

import pymysql  schemaonly = ["table1", "table2", "table6", "table9"]  db = pymysql.connect(my connection stuff)  tables = db.cursor() tables.execute("show tables") tablestuple = tables.fetchall() tableslist = []  # because there no way remove items tuple # tables.fetchall item in tablestuple:     tableslist.append(item)  schematable in schemaonly:     tableslist.remove(schematable) 

when put various print statements in code, looks proper , going work. when gets actual tableslist.remove(schematable) dreaded valueerror: list.remove(x): x not in list.

if there better way open ideas. seemed logical me iterate through list , remove items.

thanks in advance!

** edit **

everyone in comments , first answer correct. reason failing because conversion tuple list creating badly formatted list. hence there nothing matches when trying remove items in next loop. solution issue take first item each tuple , put list so: tableslist = [x[0] x in tablestuple] . once did second loop worked , table names correctly removed.

thanks pointing me in right direction!

i assume fetchall returns tuples, 1 each database row matched.

now problem elements in tableslist tuples, whereas schematable contains strings. python not consider these equal.

thus when attempt call remove on tableslist string schematable, python cannot find such value.

you need inspect values in tableslist , find way convert them strings. suspect taking first element out of tuple, not have mysql database @ hand cannot test that.


Comments