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

PythonListArraysDictionary

Python Problem Overview


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.

Python Solutions


Solution 1 - 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.

Solution 2 - Python

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

Solution 3 - Python

Use

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

Solution 4 - Python

Minor variation to user1850980's answer (for the question "How to initialize a list of empty dictionaries") using list constructor:

dictlistGOOD = list( {} for i in xrange(listsize) )

I found out to my chagrin, this does NOT work:

dictlistFAIL = [{}] * listsize  # FAIL!

as it creates a list of references to the same empty dictionary, so that if you update one dictionary in the list, all the other references get updated too.

Try these updates to see the difference:

dictlistGOOD[0]["key"] = "value"
dictlistFAIL[0]["key"] = "value"

(I was actually looking for user1850980's answer to the question asked, so his/her answer was helpful.)

Solution 5 - Python

Try this:

lst = []
##use append to add items to the list.

lst.append({'A':0,'C':0,'G':0,'T':0})
lst.append({'A':1,'C':1,'G':1,'T':1})

##if u need to add n no of items to the list, use range with append:
for i in range(n):
    lst.append({'A':0,'C':0,'G':0,'T':0})

print lst

Solution 6 - Python

I assume that motifWidth contains an integer.

In Python, lists do not change size unless you tell them to. Hence, Python throws an exception when you try to change an element that isn't there. I believe you want:

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

For what it's worth, when asking questions in the future, it would help if you included the stack trace showing the error that you're getting rather than just saying "it isn't working". That would help us directly figure out the cause of the problem, rather than trying to puzzle it out from your code.

Hope that helps!

Solution 7 - Python

Dictionary:

dict = {'a':'a','b':'b','c':'c'}

array of dictionary

arr = (dict,dict,dict)
arr
({'a': 'a', 'c': 'c', 'b': 'b'}, {'a': 'a', 'c': 'c', 'b': 'b'}, {'a': 'a', 'c': 'c', 'b': 'b'})

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAdrian RandallView Question on Stackoverflow
Solution 1 - Pythonuser1850980View Answer on Stackoverflow
Solution 2 - Pythondan04View Answer on Stackoverflow
Solution 3 - PythonTim PietzckerView Answer on Stackoverflow
Solution 4 - PythonClem WangView Answer on Stackoverflow
Solution 5 - PythonAvinashView Answer on Stackoverflow
Solution 6 - PythonDaniel StutzbachView Answer on Stackoverflow
Solution 7 - Pythonfly_manView Answer on Stackoverflow