Removing duplicates in lists Part-1
Removing duplicates in lists
In this article, let’s discuss the different ways in which we can make a unique list by removing all the duplicates present in it.
- Using a loop with append()
- Using List Comprehension
- Using List Comprehension with enumerate()
Using a loop with append():
In this method, we simply traverse the given list named original_list and append the first occurrence of the element to a new list named unique_list and ignore all the rest of the occurrences of that particular element. This ensures the duplicates aren’t repeated.
Using List Comprehension:
This method works much similar to the above method but it’s a one-liner code and a shorter version of the above. We use the keywords ‘in’ and ‘not’ in keywords to know the existence of an element in the original list and the unique list to ensure no duplicates are present.\
Using List Comprehension with enumerate():
This is one of the most popular ways to remove duplicates from a list. But one of the drawbacks is that the ordering of the elements is lost in the newly created list. The enumerate() function keeps count of the number of iterations by adding a counter to an iterable and returns this iterable as an enumerating object.