I have chosen a slightly different procedure to remove duplicates of a list. I want to keep a new list in parallel, in which each duplicate is added. Afterwards I check if the element is present in the "newly created list" in order to delete it.
The code looks like this:
# nums = [1,1,2] or [0,0,1,1,1,2,2,3,3,4]
t = []
nums_new = nums
for i in nums:
if nums[i] not in t:
t.append(nums[i])
else:
nums_new.remove(nums[i])
nums = nums_new
print(nums)
For the case when nums = [1,1,2]
this works fine and returns [1,2]
.
However, for nums = [0,0,1,1,1,2,2,3,3,4]
this case does not seem to work as I get the following output: [0, 1, 2, 2, 3, 3, 4]
.
Why is this? Can someone explain me the steps?