problem of converting a list to dict

Fredrik Lundh fredrik at pythonware.com
Wed Jan 9 20:05:39 CET 2008


Louis.Soninhu at gmail.com wrote:

> I have a list like this
> 
> mylist=['','tom=boss','mike=manager','paul=employee','meaningless']
> 
> I'd like to remove the first and the last item as they are irrevalent,
> and convert it to the dict:
> {'tom':'boss','mike':'manager','paul':'employee'}
> 
> I tried this but it didn't work:
> 
> mydict={}
> for i in mylist[1:-1]:
> 	a=i.split('=')
> 	mydict[a[0]]=a[1]
>
> and I got this:
>   File "srch", line 19, in <module>
>     grab("a/tags1")
>   File "srch", line 15, in grab
>     mydict[mylist[0]]=mylist[1]
> IndexError: list index out of range
> 
> Anyone could shed me a light on this?

works for me, with the mylist example you provided.

to see what's going on on your machine, try printing "a" after the 
split, but before you use it to populate the dictionary.

</F>




More information about the Python-list mailing list