'Why is my list getting sorted when I am sorting a list that's equivalent? Python

I am trying to set a list = to another list and then sort the new list using .sort(). However, when I print them out to make sure they stored properly, both lists got sorted when I only set the new list to be. Why is this happening? The first print statement is printing the correct unsorted list, the third statement is correctly printing the new sorted list, but the fourth print statement is supposed to be printing the unsorted list but is printing a sorted list.

def twoSum(self, nums: List[int], target: int)->List[int]:

        print("original list", nums)
        sortedList = nums
        sortedList.sort()
        subset = []
        answer = []
        middle = math.floor((len(nums)/2))
        print("middle is ", middle)
        print("sorted list", sortedList)
        print("unsorted list", nums)


Solution 1:[1]

When you assign sortedList = nums you are merely copying a reference to nums. Thus if you sort nums, sortedList will refer to that same sorted list. If you don't want that to happen, take a copy - e.g., sortedList = nums.copy()

Solution 2:[2]

You can also use the builtin sorted() function which returns a new, sorted list.

list1 = [3, 2, 1]
list2 = sorted(list1)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Albert Winestein
Solution 2 Cargo23