list to dict?

Sean Ross sross at connectmail.carleton.ca
Tue Mar 11 23:19:02 EST 2003


>>> seq = ["name=matt","sex=male","age=24"]
>>> m = dict([s.split('=') for s in seq])
>>> m
{'age': '24', 'name': 'matt', 'sex': 'male'}

or

m = dict(map(lambda s:  s.split('='), seq))

Sean


"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...






More information about the Python-list mailing list