Function to convert fetchone() result from a list to a dictio nary
sismex01 at hebmex.com
sismex01 at hebmex.com
Tue Apr 29 10:19:18 EDT 2003
> From: Alex Martelli [mailto:aleax at aleax.it]
> Sent: Tuesday, April 29, 2003 4:09 AM
>
> So here's a step-by-step version (using SPACES, not TABS:-)...:
>
> def cfd(cursor):
> values = cursor,fetchone()
> if values is None:
> return None
> keys = [d[0] for d in cursor.description]
> result_dict = dict(zip(keys, values))
> return result_dict
>
I don't think there's need to explicitly return None,
nor to assign result_dict only to immediately return
it, but then, that's just me ;-)
def cfd(cursor):
values = cursor.fetchone()
if values:
keys = [d[0] for d in cursor.description]
return dict(zip(keys,values))
> Of course, you can bunch this up more, or less, according to
> taste -- whatever you find clearest and most readable. E.g.,
> the last three lines could be instead:
>
> return dict(zip([d[0] for d in cursor.description],values))
>
> if you're into one-liners!-)
>
Ach! But Perly stuff is so unmaintainable... ;-)
> Alex
>
-gus
More information about the Python-list
mailing list