Remove duplicate items from a list with Python

 We have already validated the three ways in which items can be removed from a list, now let’s see how we can remove duplicate items from a list with Python.

In this case we return from a list of elements that we initialize with numbers:
list = [1,4,3,4,5,1,2,8,7,4,3,1,3]
Remove duplicate items with remove

If we have read the article on the ways to remove elements from a list with Python (if you should not read it now) you will have been able to verify that we have a .remove () method
which removes the first element that matches the element passed by parameter.

Its syntax would be:
list.remove (element)

In this way, if we want to eliminate the first element that matches the number 1, we would write the following:
list.remove (1)

But this only eliminates the first occurrence, what should we do to be able to eliminate all but one occurrence? Well, what we will do first is to know how many times the number appears. For this we use the .count () method
to which we pass the number to search as a parameter.
num_occurrences = list.count (1)

And now that we know how many times it appears, what we will do is use a for loop
to call the .remove () method as many times less once
for x in range (1, num_occurrences):
    list.remove (1)

When exiting the loop we will have eliminated all the numbers 1 that were in the list minus 1. Thus eliminating the duplicate elements.
Remove duplicate items with dict

Another alternative that we have to eliminate duplicate elements from the list is to use the dict object
A that represents a dictionary in Python. And we can count many features of dictionaries in Python, but the one that we will stay with is that dictionaries in Python are indexed by a key and not numerically and that key is unique.

We can create a dictionary in the following way:
dictionary = {1: ‘one’, 2: ‘two’, 3: ‘three’};

In our case we are going to transform the list into a dictionary using the .fromkeys () method
what it does is take the elements of a list and convert them into the keys of a dictionary.
dict.fromkeys (list)

In this conversion duplicate keys will be eliminated

Now what we will do is convert this dictionary back to a list using the .list () method
and again we will have the list, in this case having eliminated the duplicate elements.
list = list (dict.fromkeys (list))
print (list)

With this we have already seen the two options we have to eliminate duplicate elements from a list with Python. Can you think of another way to do it? Let us know in the comments.

Be the first to comment

Leave a Reply

Your email address will not be published.


*