This is more of a best practice question. What I have created is working perfectly, but I am curious if there is a shorter method for creating a dictionary out of my current data structure.
I am reading tables out of a SQLite database, the data is returned as a list of tuples. eg
[(49, u'mRec49', u'mLabel49', 1053, 1405406806822606L, u'1405406906822606'),
(48, u'mRec48', u'mLabel48', 1330, 1405405806822606L, u'1405405906822606'),
(47, u'mRec47', u'mLabel47', 1220, 1405404806822606L, u'1405404906822606')...
]
I want to take each column of the list-tuple structure, make it into a list, get the column name from the database and use that as the key holding the list. Later I turn my dictionary into JSON.
Here is my function I scratched up, it does the job, I just can't help wondering if there is a better way to do this.
def make_dict(columns, list_o_tuples):
anary = {}
for j, column in enumerate(columns):
place = []
for row in list_o_tuples:
place.append(row[j])
anary[column] = place
return anary
make_dict(mDBTable.columns, mDBTable.get_table())
Note:the function shouldn't care about the table its presented, or the number or rows & columns in table.