problem of converting a list to dict

Louis.Soninhu at gmail.com Louis.Soninhu at gmail.com
Wed Jan 9 14:25:29 EST 2008


On Jan 9, 3:05 pm, Fredrik Lundh <fred... at pythonware.com> wrote:
> Louis.Soni... 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>- Hide quoted text -
>
> - Show quoted text -

'print a' works



More information about the Python-list mailing list