remove duplicate elements from lists using python

In this example, you will learn to remove duplicate elements from list with different methods.

Remove duplicates from list using Set

To remove duplicates from list, you can use built-in library function set(). The set() method will return distinct elements from list. You just need to pass list to set method in order to find unique or remove duplicates.

Example :

    
# Program to remove duplicates from list using set method
dataList = [1,2,1,3,5,6,8,5,9,3]

#Removing Duplicates
uniqueList = set(dataList)

print(list(uniqueList))
   

Output :

    
[1, 2, 3, 5, 6, 8, 9]
    

Remove Duplicates from a list using the Temporary List

In this method you need to define temporary list and loop through list having duplicate elements. While looping you need to check element is exist in temporary list or not and add unique elements into temporary list.

Example :

    
# Program to remove duplicates from list using Temporary List
dataList = [1,2,1,3,5,6,8,5,9,3]
tempList = []

for x in dataList:
    if x not in tempList:
        tempList.append(x)

print(list(tempList))
   

Output :

    
[1, 2, 3, 5, 6, 8, 9]
   

Remove duplicates from list using Numpy unique() method.

The method unique() from Numpy module can help us remove duplicate from the list given. For using this function we need to import numpy module in our program.

Example :

    
# Program to remove duplicates from list using Numpy unique() method
import numpy as np

dataList = [1,2,1,3,5,6,8,5,9,3]
uniqueList = np.unique(dataList).tolist()
print(list(uniqueList))
   

Output :

    
[1, 2, 3, 5, 6, 8, 9]
   

Share your thoughts

Ask anything about this examples