This is a sample list (each line has variable elements) :
['1', 'Tech', 'Code']
['2', 'Edu']
['3', 'Money', 'Sum', '176']
I have to insert this into a MySQL table which has 4 columns (max num. of elements in a value in a list).
How to do this efficiently? I have a feeling my solution is the least efficient!
Here is my solution :
for eachval in mylistings: #mylistings has the sample list elements described above
mylen = len(eachval)
defaultlist = [None]*4 #reset defaultlist to 'None' to handle variable no. of columns
ctr = 0
for myoneval in mylistingline:
defaultlist[ctr] = myoneval
ctr += 1
for finalval in defaultlist: #finally inserting them into a MySQL table
cursor.execute("INSERT INTO LoadData VALUES (%s, %s, %s, %s)", (finalval[0], finalval[1], finalval[2], finalval[3]))