How can I create an array/list of dictionaries in python? How can I create an array/list of dictionaries in python? arrays arrays

How can I create an array/list of dictionaries in python?


This is how I did it and it works:

dictlist = [dict() for x in range(n)]

It gives you a list of n empty dictionaries.


weightMatrix = [{'A':0,'C':0,'G':0,'T':0} for k in range(motifWidth)]


Use

weightMatrix = []for k in range(motifWidth):    weightMatrix.append({'A':0,'C':0,'G':0,'T':0})