Fastest way to convert sql result into a dict or list ?

rewonka at gmail.com rewonka at gmail.com
Wed Oct 29 07:35:31 EDT 2008


Hello,

I'm trying to find the fastest way to convert an sql result into a
dict or list.
What i mean, for example:
my sql result:
contact_id, field_id, field_name, value
sql_result=[[1, 1, 'address', 'something street'],
                 [1, 2, 'telnumber', '1111111111'],
                 [1, 3, 'email', 'something at something.net'],
                 [2, 1, 'address','something stree'],
                 [2, 3, 'email','something at something.net']]
the dict can be:
dict={1:['something street', '1111111111' ,
'something at something.net'],
        2:['something street', '', 'something at something.net' ]}
or a list can be:
list=[[1,'something street', '1111111111' ,
'something at something.net'],
       [2,'something street', '', 'something at something.net' ]]

I tried to make a dict, but i think it is slower then make a list, and
i tried the "one lined for" to make a list, it's look like little bit
faster than make a dict.

def empty_list_make(sql_result):
    return [ [line[0],"", "", ""]   for line in sql_result]

than fill in the list with another for loop.
I hope there is an easyest way to do something like this ??
any idea ?



More information about the Python-list mailing list