list 2 dictionary newcomer question
Andy Todd
atodd at spam.free.email.com
Tue Jun 19 21:57:12 EDT 2001
More information about the Python-list mailing list
Tue Jun 19 21:57:12 EDT 2001
- Previous message (by thread): list 2 dictionary newcomer question
- Next message (by thread): RFD comp.lang.python.zope & ZPugDC Meeting Tomorrow
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
t_messmer at yahoo.com (Tom Messmer) wrote in <f74c89ea.0106191351.32dd9cdd at posting.google.com>: >Lets say I have a list of arbitrary, but definitely even length, and I >want to convert it to a dictionary. List might look like ['/', '15', >'/proc', '100'](I'm parsing the output of df) I'd simply like to pair >'em up like {'/':'15', '/proc':'100'} I have a feeling I'm going to be >embarrassed at how easy this is, but I cant seem to figure it out. >I've seen examples on doing this with lists of lists, but not flat >lists. Any takers? >Tom My two cents, barring an automatic coercion (which I haven't looked for in the documentation - sorry) is; >>> list=['a', 1, 'b', 2, 'c', 3] >>> dict={} >>> x=len(list)-1 >>> for i in range(0,x): ... if i%2==0: ... dict[list[i]]=list[i+1] ... >>> dict {'b', 2, 'c', 3, 'a', 1} Hope that helps, Andy -- Content free posts a speciality
- Previous message (by thread): list 2 dictionary newcomer question
- Next message (by thread): RFD comp.lang.python.zope & ZPugDC Meeting Tomorrow
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list