i was doing an assignment using Python and i had to use a 2D list with a specific size.
my idea was to use this..
lists = [[]]*10 //output => [[],[],[],[],[],[],[],[],[],[]]
because i have used..
nums = [0]*10 //output [0,0,0,0,0,0,0,0,0,0]
so far so good, the problem was wwhen i do this..
lists[0].append(5) //the result is [[5],[5],[5],[5],[5],[5],[5],[5],[5],[5]]
it appends the object i'm adding to every list in lists, while if i did this..
nums[0] = 5 //the result is [5,0,0,0,0,0,0,0,0,0]
i have figured a work around the problem by using
lists = [[] for i in range(10)]
but my question is: why it did that, in first case?