Splitting string into dictionary

George Sakkis gsakkis at rutgers.edu
Fri Jul 1 01:33:19 EDT 2005


"Robert Kern" wrote:

> Ignore the last message.
>
> translations = [x.strip(" '") for x in line.split('|')]
> d = dict(zip(translations[::2], translations[1::2]))

Or in case you're working with a lot and/or huge records, the second
line can be more efficiently written as:

from itertools import izip, islice
d = dict(izip(islice(translations,0,None,2),
              islice(translations,1,None,2)))

George




More information about the Python-list mailing list