To my knowledge, list.append(item)
in Python adds an item to the end of the existing list.
Now, considering the following program:
numlist=[1,2,3]
vallist = ["a","b","c"]
db = []
card = ["number","value"]
for i in range(len(numlist)):
card[0]=numlist[i]
card[1]=vallist[i]
db.append(card)
print(db)
I was expected the output [[1, 'a'],[2, 'b'],[3, 'c']]
,
But, to my surprise, the output came as: [[3, 'c'], [3, 'c'], [3, 'c']]
I have tried, visualizing the aforementioned program using Python Tutor:
But I don't understand, why all 3 indices of the list indicate the last input of the list. What am I missing here? Also, how can I get my desired output using the list append method?
Thank you.