In this tutorials, we will see how to remove duplicate elements from a python list.

Remove duplicate elements from List :

We can accomplish this using different ways, let’s see all the ways.

The list of elements is reading from user input.

Example 1 :

Removing duplicates using membership operators in python.

elem = eval(input("Enter List of values : "))
list1=[]
for x in elem:
    if x not in list1:
        list1.append(x)
print("After Removing duplicate elements ",list1)

Output:

Enter List of values : [10,20,30,40,10,20,50,60]
After Removing duplicate elements  [10, 20, 30, 40, 50, 60]

Example 2:

Removing duplicate elements using Python’s list comprehension.

list1=[10,20,30,10,50,20,10,60,80,50,40,10]

newlist=[ele for n,ele in enumerate(list1) if ele not in list1[:n]]

print("New List : ",newlist)

Output:

New List :  [10, 20, 30, 50, 60, 80, 40]

Example 3:

Removing duplicates usind sorted() function.

list1 = ['a','c','a','d','e','a','f','b']  
list2 = sorted(set(list1),key=list1.index)  
print(list2)

Output:

['a', 'c', 'd', 'e', 'f', 'b']

Example 4:

If you are concerning about insertion order, you can also remove duplicate elements using python set data structure.

elem = eval(input("Enter List of values : "))
set1 = set(elem)
print("After Removing duplicate elements ",set1)

Output:

Enter List of values : [1,2,3,4,5,1,2,6,4]
After Removing duplicate elements  {1, 2, 3, 4, 5, 6}

Example 5:

Let’s consider, our list containing string elements. We can remove the duplicates from below set functions.

list1 = ["abc", "xyz", "pqr"]
list2 = ["aei", "oup", "xyz","abc"]

newSet= set(list1).symmetric_difference(list2).union(set(list1).intersection(list2))

print(newSet)

Output:

You can find more explanation about the output, from our earlier example of the set data structure.

{'pqr', 'aei', 'abc', 'xyz', 'oup'}

 

Reference:

Happy Learning 🙂

About the Author:

Avatar
Founder of onlinetutorialspoint.com Love Java, Python, Shell and opensource frameworks. Follow him on twitter and facebook for latest updates.

Leave A Comment