This is my python code (I am using Python 2.7.6):
row_of_words = ['word 1', 'word 2']
hw = ['h1', 'h2']
TD = {}
TD = TD.fromkeys(hw, [])
# TD is now {'h1': [], 'h2': []}
for index in range(len(row_of_words)):
# This loops twice and in the first loop, index = 0 and in the second
# loop, index = 1
# Append 1 to the value of 'h1' (which is a list [])
# The value for 'h1' should now be [1].
# This loops twice to append 1 twice to the value of 'h1'.
TD['h1'].append(1)
>>>TD
{'h2': [1, 1], 'h1': [1, 1]}
As you can see in the two lines right above, when I print TD
, it shows that the 1
was appended to both h1
and h2
. Any idea why it was appended to both even though I mentioned to only append it to TD['h1']
? How do I make it so that it only appends to the value of 'h1'? I commented my steps showing how I think it works.