In this article we will discuss different ways to remove duplicate elements from a list in python.

List : Containing duplicate elements :

List : After removing duplicate elements,

Let’s see how to do that,

Remove duplicates from a List using set

Set is an un-ordered data structure that contains only unique elements.

Now suppose we have a list that contains duplicate elements i.e.

Let’s create a set with this list. So, only unique elements will be added in the set. Then convert this set again to a list i.e.

So, now list will contains only unique elements i.e.

But, order of elements is disturbed.

This technique removes the duplicates but it does not keep the elements in same order as original.
If we want to keep the unique elements of list in same order as original, then we need to use other technique.

Removing duplicates from a List by keeping the order

Suppose we have a list that contains duplicate elements i.e.

Now we want to remove duplicates from it and also want to keep the order of unique elements as it was in original list i.e.

Unique elements : [10, 2, 45, 3, 5, 7, 8]

To do that we need to create a new list for unique elements. Then iterate over the original list and for each element in the list, add it to new uniqueList only if its not already in the list.

Let’s use the above function to remove duplicates from list i.e.

Contents of the list will be,

Complete example is as follows,




Click Here to Subscribe for more Articles / Tutorials like this.