problem of converting a list to dict

Louis.Soninhu at gmail.com Louis.Soninhu at gmail.com
Wed Jan 9 13:56:36 EST 2008


Hi pals

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('=')		# this will disect each item of mylist into a 2-item
list
	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?

thanks



More information about the Python-list mailing list