Remove duplicate of a list via list matching in Python
Question:
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?
Answers:
There are two issues with your code:
-
Since you are iterating over a list, for i in nums
, i
is your actual number, not the indexer, so you should just use i
instead of nums[i]
.
-
nums_new = nums
will not actually make a copy of nums
, but instead it will make a second binding to the very same list. So you should write nums_new = nums.copy()
to avoid changing your original list while you iterate over it.
With those two changes your code works as you wish:
nums = [0,0,1,1,1,2,2,3,3,4]
t = []
nums_new = nums.copy()
for i in nums:
if i not in t:
t.append(i)
else:
nums_new.remove(i)
nums = nums_new
print(nums)
Returns [0,1,2,3,4]
.
Of course this is an academic exercise of some kind, but note that the Pythonic way to remove duplicates from a list is:
list(dict.fromkeys(nums))
if you want to preserve the order, and
list(set(nums))
for a slightly faster method if you do not care about order.
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?
There are two issues with your code:
-
Since you are iterating over a list,
for i in nums
,i
is your actual number, not the indexer, so you should just usei
instead ofnums[i]
. -
nums_new = nums
will not actually make a copy ofnums
, but instead it will make a second binding to the very same list. So you should writenums_new = nums.copy()
to avoid changing your original list while you iterate over it.
With those two changes your code works as you wish:
nums = [0,0,1,1,1,2,2,3,3,4]
t = []
nums_new = nums.copy()
for i in nums:
if i not in t:
t.append(i)
else:
nums_new.remove(i)
nums = nums_new
print(nums)
Returns [0,1,2,3,4]
.
Of course this is an academic exercise of some kind, but note that the Pythonic way to remove duplicates from a list is:
list(dict.fromkeys(nums))
if you want to preserve the order, andlist(set(nums))
for a slightly faster method if you do not care about order.