module for working with the result set

john fabiani jfabiani at yolo.com
Tue May 18 10:10:45 EDT 2004


Diez B. Roggisch wrote:
>>OK thats great!  I did the following and it works.
>>for field in range(0,len(mydata[0])):
>>   print mydata[0][field]
>>following your thoughts on the keys and my mistake - can you tell me how
>>to get into a dict format?
> 
> 
> Its a one-liner: If fnames is the list of column names (remember you can get
> these from the cursor) and row is your data, this will create a dict out of
> them:
> 
> d = dict(zip(fnames, row))
> 
> Thats all.
> 
> 
I'm missing something very important about list and dict because your 
one liner does not work.  This is what I did:

myfields=mycur.description
#since I already had the data I used it.
d = dict(zip(myfields,mydata[0]))
Traceback (most recent call last):
   File "<input>", line 1, in ?
TypeError: list objects are unhashable


So "myfields" is a list within a list.
I'm able to list the fields with the following:
for count in range(0,len(myfields)):
   print myfields[count][0]

The above will print the fields name - which is what I want to become my 
keys.  So now I have to make what is printed into a list.

mylist=[]
for count in range(0,len(myfields)):
    mylist.append(myfields[count][0])
now I have a single list.
['csono', 'crevision',............]

then I'd expect your code to work but I get a list of tuples
[('csono', '5992      '), ('crevision', 'A') ......]

What I expected to see was
{'csono': '5992      '...}

So now I'm completely confused....  But I guess I'm learning....
John



More information about the Python-list mailing list