I am writing a program to remove the duplicated elements from a given sorted list. I wrote function "removeDuplicates" to do some modifications on the list and print the updated list at the end of the function. Since the list is passed by reference, the question is why the changes do not apply on the list outside the function.
def removeDuplicates(nums):
c = 0
nums = nums + [nums[-1] + 1] # add a dummy element to the end of the list
for i in range(len(nums) - 1):
if nums[i] != nums[i + 1]:
nums[c] = nums[i]
c = c + 1
nums.pop()
print(nums)
if __name__ == "__main__":
nums = [1, 1, 1, 2, 2, 3, 4, 4, 4, 5, 5]
removeDuplicates(nums)
print(nums)