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

arrays, dictionary, list, python

Solution

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.

Problem

I have a dictionary as follows: ``` {'A':0,'C':0,'G':0,'T':0} ``` I want to create an array with many dictionaries in it, as follows: ``` [{'A':0,'C':0,'G':0,'T':0},{'A':0,'C':0,'G':0,'T':0},{'A':0,'C':0,'G':0,'T':0},...] ``` This is my code: ``` weightMatrix = [] for k in range(motifWidth): weightMatrix[k] = {'A':0,'C':0,'G':0,'T':0} ``` But of course it isn't working. Can someone give me a hint? Thanks.

Original source

Related problems