Removing Duplicate Elements from a List in Python
2 min read
In this tutorial, you will learn how to remove duplicate elements from a list in Python. Let's get started with the code. If you find this article helpful, please consider sharing it with your coding communities and social media groups so that others can benefit from the content as well.
To remove duplicates from a list, we will follow this logic:
Iterate through the elements of the list one by one.
Create a temporary, empty list called
For each element
xin the list, check if it is already present in the temporary list
If it is not present, append it to the list.
Here is the code for this process:
# Original list original_list = [2,4,6,2,4,9,2,5,8] # Temporary, empty list to store unique elements temp_list =  # Iterate through the elements of the original list for x in original_list: # Check if the element is not already in the temporary list if x not in temp_list: # If not, append it to the list temp_list.append(x) # Set the new list equal to the temporary list new_list = temp_list # Print the new list print(new_list)
Running this code will give us the following output:
[2, 4, 6, 9, 5, 8]
As you can see, the duplicate elements (2, 4) have been removed from the list.
I hope this code and explanation are helpful in understanding how to remove duplicates from a list in Python. Thank you for reading!