Creating a list of dictionaries in python

Viewed 1188

I have a following data set that I read in from a text file:

all_examples=   ['A,1,1', 'B,2,1', 'C,4,4', 'D,4,5']

I need to create a list of dictionary as follows:

lst = [ 
{"A":1, "B":2, "C":4, "D":4 },
{"A":1, "B":1, "C":4, "D":5 } 
]

I tried using an generator function but it was hard to create a list as such.

attributes = 'A,B,C'
def get_examples():

    for value in examples:
        yield dict(zip(attributes, value.strip().replace(" ", "").split(',')))
4 Answers
Related