How to Remove Duplicate Elements from a List in Python?

A list in Python can contain any number of duplicate elements. However, sometimes it is required to make elements in the list unique. In this tutorial, we will show you different ways to remove duplicate elements from a list in Python.

Using Python Built-in set() function

Using the Python built-in set() function is the most simple way of removing duplicates from a list in Python.

Example:

fruit_list =  ["apple", "mango", "apple", "banana", "mango", "apple", "grapes"]

unique_list = list(set(fruit_list))
print(unique_list)
Output:
['mango', 'banana', 'apple', 'grapes']

Using List Comprehension

The following is a one line code that uses list comprehension to remove duplicate elements from a Python list.

Example:

fruit_list =  ["apple", "mango", "apple", "banana", "mango", "apple", "grapes"]

unique_list = []
[unique_list.append(n) for n in fruit_list if n not in unique_list]
print(unique_list)
Output:
['apple', 'mango', 'banana', 'grapes']

Using Loop

To remove duplicate elements from a list, we can also loop through the list and append only the first occurrence of the element in a new list.

Example:

fruit_list =  ["apple", "mango", "apple", "banana", "mango", "apple", "grapes"]

unique_list = [] 
for item in fruit_list: 
    if item not in unique_list: 
        unique_list.append(item) 

print(unique_list)
Output:
['apple', 'mango', 'banana', 'grapes']

Using OrderedDict.fromkeys()

If you want to remove duplicate elements and also maintain the insertion order of elements in a list then you can use the OrderedDict.fromkeys() function.

Example:

from collections import OrderedDict

fruit_list =  ["apple", "mango", "apple", "banana", "mango", "apple", "grapes"]
unique_list = list(OrderedDict.fromkeys(fruit_list))
print(unique_list)
Output:
['apple', 'mango', 'banana', 'grapes']