A list can have duplicate elements present in it and if we want to delete the duplicate elements, then there are several ways available. We would look at couple of those techniques:
1) Drop Duplicates from Python List using Python set:
Python set has a property that even if you add duplicate elements to it, the set only holds the unique items and ignores the duplicate elements by default.
Therefore, first we apply a set on the list which will convert a list to a set and all the duplicate elements will be dropped and finally we will convert the set back to a list. So, ultimately the list that we will get will not have any duplicate values in it.
Code:
lst = [ 6, 3, 1, 2, 1, 5, 8, 3, 2, 7, 4, 7, 3, 3, 4, 1, 2, 2]
lst = list(set(lst))
lst
Output:
[1, 2, 3, 4, 5, 6, 7, 8]
2) Drop Duplicates from Python List without using Python set:
It can be a good interview question. Sometimes an interviewer asks you to drop the duplicate elements from the list without using Python set functionality.
This can be achieved by taking one empty list and one by one elements will be inserted into this list. If an item is already present in this new list, then it will not be inserted again.
Hence we will have only unique elements in the new list finally.
Code:
lst = [ 6, 3, 1, 2, 1, 5, 8, 3, 2, 7, 4, 7, 3, 3, 4, 1, 2, 2]
new_lst = []
for item in lst:
if item not in new_lst:
new_lst.append(item)
new_lst
Output:
[6, 3, 1, 2, 5, 8, 7, 4]
We have many more cool tricks, stay tuned.
We collect cookies and may share with 3rd party vendors for analytics, advertising and to enhance your experience. You can read more about our cookie policy by clicking on the 'Learn More' Button. By Clicking 'Accept', you agree to use our cookie technology.
Our Privacy policy can be found by clicking here