How do you remove duplicates from a list in Python?
Posted by MaryJns
Last Updated: August 21, 2024
Removing duplicates from a list in Python can be accomplished using several methods. Each method has its advantages and use cases depending on the requirements, such as maintaining the order of elements or optimizing for performance. Here are some common approaches:
1. Using set()
The simplest way to remove duplicates from a list is by converting it to a set, as sets inherently do not allow duplicate values. However, this method does not preserve the original order of elements.
original_list = [1, 2, 2, 3, 4, 4, 5]
unique_list = list(set(original_list))
2. Using List Comprehension
This approach maintains the order of elements by iterating through the list and only including elements that have not been encountered before.
original_list = [1, 2, 2, 3, 4, 4, 5]
unique_list = []
[unique_list.append(x) for x in original_list if x not in unique_list]
3. Using Dictionary Keys
In Python 3.7 and later, dictionaries maintain insertion order. Therefore, using a dictionary's keys can preserve the order while removing duplicates.
original_list = [1, 2, 2, 3, 4, 4, 5]
unique_list = list(dict.fromkeys(original_list))
4. Using pandas
For those who are already using the Pandas library, removing duplicates can be done easily with the drop_duplicates() method.
import pandas as pd

original_list = [1, 2, 2, 3, 4, 4, 5]
unique_list = pd.Series(original_list).drop_duplicates().tolist()
Conclusion
Each of these methods serves different needs. If maintaining order is essential, using list comprehension or dictionary keys is recommended. For quick deduplication and when order does not matter, converting to a set is most efficient. Select the approach that best suits your situation based on the importance of order and performance considerations.