list to dict?
Adonis
adonisv at earthlink.net
Tue Mar 11 22:08:07 EST 2003
"Matt Neuber" <mneuber at attbi.com> wrote in message
news:8fc3c459.0303111859.7ebbd60a at posting.google.com...
> This is probably a pretty "newbish" question but...
>
> Say I have a list, and it looks something like
> ["name=matt","sex=male","age=24"] (etcetera) and I wanted to convert
> it to a dictionary: {'name': 'matt', 'sex': 'male', 'age': '24'} and
> so forth.
> Is there an easy way to do this? I've tried a number of things, but I
> have run into problems because of the literals...
You can try:
>>> x = ["name=matt", "sex=male", "age=24"]
>>> y = {}
>>> for z in x: y[z.split('=')[0]] = z.split('=')[1]
...
>>> y
{'age': '24', 'name': 'matt', 'sex': 'male'}
>>>
Although someone else might present a even better alternative?
Hope this helps...
Adonis
More information about the Python-list
mailing list