I have a list
['Tests run: 1', ' Failures: 0', ' Errors: 0']
I would like to convert it to a dictionary as
{'Tests run': 1, 'Failures': 0, 'Errors': 0}
How do I do it?
I have a list
['Tests run: 1', ' Failures: 0', ' Errors: 0']
I would like to convert it to a dictionary as
{'Tests run': 1, 'Failures': 0, 'Errors': 0}
How do I do it?
output:
Loop over your list, and split by the colon. Then assign the first value to the second value in a dict object:
naive solution assuming you have a clean dataset:
This assumes that you do not have duplicates and you don't have other colons in there.
What happens is that you first split the strings into a tuple of two values. You do this here with a generator expression. You can pass this directly into the dict, since a dict knows how to handle an iterable yielding tuples of length 2.
Try this
Result