Reputation: 5014
I have implemented an approach to create a list of dictionaries, but I was wondering if there was a more efficient method of doing so (while preserving the order elements in both lists):
#global variable
dict_agriculture = []
col_name_agriculture = [tuple[0] for tuple in querycurs.description]
rows_agriculture = querycurs.fetchall()
for row in rows_agriculture:
dict1 = dict(zip(col_name_agriculture, list(row)))
dict_agriculture.append(dict1)
Upvotes: 1
Views: 137
Reputation: 42090
I'm using sqlite.
In that case, the most efficient way would be to set row_factory
to the built-in Row
type, which is implemented in C, and will be faster than any equivalent code in Python.
For example...
import sqlite3
con = sqlite3.connect(...)
con.row_factory = sqlite3.Row
# Now queries will return objects which act as both a tuple, and a dictionary,
# so you can just do...
querycurs = con.cursor()
querycurs.execute(...)
dict_agriculture = querycurs.fetchall()
Upvotes: 2
Reputation: 7332
If I read this right, you have a query cursor, where the .description property is a list of tuples with field names as the first item. You then want to read the rows, turning each row list into a row dict using the row names from the tuple.
This can be done with a list comprehension:
col_name_agriculture = [tuple[0] for tuple in querycurs.description]
dict_agriculture = [dict(zip(col_name_agriculture, row) for row in querycurs]
Consider also how the final list will be used - a generator expression may be usable there if it is to be used in order.
Upvotes: 1
Reputation: 1124848
You could use a list comprehension here; its more efficient at creating the final list. Don't use .fetchall()
when iteration will do, don't use tuple
as a variable name, and there is no need to call list
on each row:
fields = [tup[0] for tup in querycurs.description]
dict_agriculture = [dict(zip(fields, row)) for row in querycurs]
Upvotes: 4