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.
Remove Duplicate Items From a Python List
Okay, let us see the simple solution. See the following code.
# app.py GoT = ['Tony', 'Rocket', 'Scott', 'Steve', 'Rocket', 'Natasha', 'Tony'] mylist = list(dict.fromkeys(GoT)) print(mylist)
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.
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.
# app.py def unique(data): return list(dict.fromkeys(data)) GoTB = ['Tony', 'Rocket', 'Scott', 'Steve', 'Rocket', 'Natasha', 'Tony'] finalList = unique(GoTB) print(finalList)
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.
# app.py def duplicate(items): unique =  for item in items: if item not in unique: unique.append(item) return unique GoT = ['Tony', 'Rocket', 'Scott', 'Steve', 'Rocket', 'Natasha', 'Tony'] print(duplicate(GoT))
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.
# app.py GoTC = ['Tony', 'Rocket', 'Scott', 'Steve', 'Rocket', 'Natasha', 'Tony'] finalData = list(set(GoTC)) print(finalData)
The set will remove the duplicate items from the list, but the output will be unordered. See the output.
Finally, How To Remove Duplicate Items From a Python List Tutorial With Example is over.