>>> AB
['1', '3', '1', '3']
>>> BC = AB
>>> BC
['1', '3', '1', '3']
>>> BC.sort()
>>> BC
['1', '1', '3', '3']
>>> AB
['1', '1', '3', '3']
I wanted to sort only list BC. Why it sorted list AB as well and how to prevent it?
You are basically pointing to the same area in the program's memory with AB and with BC. So, to create BC as a new list, and save it in a different place in the memory, you can do this:
Assuming this is python:
BC = AB[:] # creates a copy of AB
Now, when you modify BC, it will not change AB. Sorting will have no affect on AB this way. Now they point to different places in the memory.
It could be possible that BC and AB are pointing to the same object in memory and variable holder AB and BC act like a pointer. They both are pointing to the same thing and so if you sort BC, the list BC is pointing to gets sorted and AB also points to the now sorted list BC.
To correct this, instead of saying BC = AB
you can say BC = AB[:]
which will store a copy of AB in BC instead of AB itself. This will result in: changes made to one not being reflected in the other.