Easy way to Remove Duplicates from a Python list

In this tutorial, you will learn how can easily remove duplicate elements from a Python list.

Remove Duplicates from a Python list

Method: 1

In the following example, you can see that the list is converted to a set and then converted back to a list.

>>> a = [35, 'Java', 'Python', 52, 'PHP', 'C++', 35, 'C++', 'Python']
>>> b = list(set(a))
>>> print(b)
[35, 'PHP', 52, 'Java', 'Python', 'C++']
>>>

But, there is some issue with the above method –

  • It does not maintain the order of the elements.
  • This method won’t work if the list contains any mutable objects.

Method: 2

In the second method, you have to check each item of the list through a loop.

In the following code you can see that a function is defined called remove_dup() to remove the duplicate elements.

This function takes a List as a parameter whose duplicate elements will be removed, and it returns a new list with unique elements.

a = [35, 'Java', 'Python', 52, 'PHP', 'C++', 35, 'C++', 'Python', {1, 5, 3, 5, 3, 7, 5, 9}]

def remove_dup(the_list):
    tempList = []
    for i in the_list:
        if i not in tempList:
            tempList.append(i)
    return tempList

print(remove_dup(a))
[35, 'Java', 'Python', 52, 'PHP', 'C++', {1, 3, 5, 7, 9}]

Leave a Reply