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.
A dictionary in Python constitutes a group of elements in the form of key-value pairs. A list can store elements of different types under a common name and at specific indexes. In Python, we can have a list or an array of dictionaries.
In Python, you can have a List of Dictionaries. You already know that elements of the Python List could be objects of any type. In this tutorial, we will learn how to create a list of dictionaries, how to access them, how to append a dictionary to list and how to modify them.
Two arrays are required to create a dictionary. One array is used as keys and the other array is used as values. The size of both the arrays should be same, thus making a dictionary with size same as that of the array. Following is the syntax to create a dictionary from two Arrays.
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)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With