list 2 dictionary newcomer question

Andy Todd atodd at spam.free.email.com
Tue Jun 19 21:57:12 EDT 2001


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



More information about the Python-list mailing list