This is because both list and list2 belong to the same list after completing the list2=list job.
Try this to see if they apply to the same objects or to others:
id(list) id(list2)
Example:
>>> list = [1, 2, 3, 4, 5] >>> list2 = list >>> id(list) 140496700844944 >>> id(list2) 140496700844944 >>> list.remove(3) >>> list [1, 2, 4, 5] >>> list2 [1, 2, 4, 5]
If you really want to create a duplicate copy of list so that list2 does not refer to the original list but to the copy of the list, use the slice operator:
list2 = list[:]
Example:
>>> list [1, 2, 4, 5] >>> list2 [1, 2, 4, 5] >>> list = [1, 2, 3, 4, 5] >>> list2 = list[:] >>> id(list) 140496701034792 >>> id(list2) 140496701034864 >>> list.remove(3) >>> list [1, 2, 4, 5] >>> list2 [1, 2, 3, 4, 5]
Also, do not use list as the name of the variable, because initially the list refers to a list of types, but by defining your own variable list , you hide the original list that refers to the list of types. Example:
>>> list <type 'list'> >>> type(list) <type 'type'> >>> list = [1, 2, 3, 4, 5] >>> list [1, 2, 3, 4, 5] >>> type(list) <type 'list'>