May-24-2017, 10:35 AM
my question here
I was experimenting with lists and this particular behavior of python caused me some confusion. Can anyone please clarify if what i think is right.
suppose i make a small change like this:
Since all 3 lists are one single list object with 3 references is this happening because in the first case I'm actually assigning a new object to the first position and in the second case I'm modifying the object that all 3 lists are referring to?
I was experimenting with lists and this particular behavior of python caused me some confusion. Can anyone please clarify if what i think is right.
myList = [[1,2,3]] * 3 myList[0]=[1,2,3,4]now when i print myList i get the following output: [[1,2,3,4],[1,2,3],[1,2,3]]
suppose i make a small change like this:
myList = [[1,2,3]] * 3 myList[0].append(4)now when i print myList i get the following output: [[1,2,3,4],[1,2,3,4],[1,2,3,4]]
Since all 3 lists are one single list object with 3 references is this happening because in the first case I'm actually assigning a new object to the first position and in the second case I'm modifying the object that all 3 lists are referring to?