Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove duplicate of a list via list matching in Python

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].

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

There are two issues with your code:

  1. 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].

  2. 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.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading