Convert list of strings to dictionary

Use:

a = ['Tests run: 1', ' Failures: 0', ' Errors: 0']

d = {}
for b in a:
    i = b.split(': ')
    d[i[0]] = i[1]

print d

returns:

{' Failures': '0', 'Tests run': '1', ' Errors': '0'}

If you want integers, change the assignment in:

d[i[0]] = int(i[1])

This will give:

{' Failures': 0, 'Tests run': 1, ' Errors': 0}

Leave a Comment