Latest Code Tutorials

How To Remove Duplicate Items From a Python List


In this tutorial, we will see How To Remove Duplicate Items From a Python List. Pretty much in every project, you need to write a code to check if the list has any duplicates and if it has copies, then we need to remove them and returns the new list with the unique items. In Python, there are lots of ways you can do this. You can either use the Python Dictionary to remove it or write the user-defined function that can remove the duplicate entry and gives the unique items list.

If you want to learn more about Python then check out this course Learn Python Programming Masterclass

Remove Duplicate Items From a Python List

Okay, let us see the simple solution. See the following code.


GoT = ['Tony', 'Rocket', 'Scott', 'Steve', 'Rocket', 'Natasha', 'Tony']
mylist = list(dict.fromkeys(GoT))

First, we have created a dictionary, using the List items like keys. It will automatically remove any duplicates because dictionaries data structure cannot have duplicate keys. The final step will be to convert the dictionary back into the list. Now we have the List without any copies, and it has the same order as the original List.

Print the List to demonstrate the result. See the below output.


Remove Duplicate Items From a Python List

So, it removes the duplicate items from Python List and get the list of unique items.

Create a Function

If you like to have the function where you can pass your lists as an argument, and get back the list without duplicates, you can create the function. See the below example.


def unique(data):
  return list(dict.fromkeys(data))

GoTB = ['Tony', 'Rocket', 'Scott', 'Steve', 'Rocket', 'Natasha', 'Tony']
finalList = unique(GoTB)

Here, we have written the logic inside the unique function, and that function returns the unique values of the list. It will give us the same output.

Using Not In On List To Remove Duplicate Elements

We can use not in on the list to find out the duplicate items. We create a result list and insert only those that are not in the unique list. See the following code.


def duplicate(items): 
    unique = [] 
    for item in items: 
        if item not in unique: 
    return unique 

GoT = ['Tony', 'Rocket', 'Scott', 'Steve', 'Rocket', 'Natasha', 'Tony']

It gives the same output with the unique values of the list.

Remove duplicates from a List using a set

Python Set is an unordered data structure that contains unique elements.

See the following code.


GoTC = ['Tony', 'Rocket', 'Scott', 'Steve', 'Rocket', 'Natasha', 'Tony']
finalData = list(set(GoTC))

The set will remove the duplicate items from the list, but the output will be unordered. See the output.

Remove duplicates from a List using a set


Finally, How To Remove Duplicate Items From a Python List Tutorial With Example is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.