Home >Backend Development >Python Tutorial >How to Efficiently Remove Duplicates from a Python List While Maintaining Order?
Removing Duplicates in Lists
In Python, checking for duplicates in a list and returning a new list without them is a common task. There are several approaches to accomplish this.
Using Sets
Sets are unordered collections of distinct objects. They provide an efficient way to remove duplicates. To create a set from a list, simply pass it to the set() function. To convert it back to a list, use the list() function. This method, however, does not preserve the original order of the elements.
t = [1, 2, 3, 1, 2, 3, 5, 6, 7, 8] list(set(t)) # [1, 2, 3, 5, 6, 7, 8]
Maintaining Order
If preserving the original order is crucial, the collections.OrderedDict module can be used. OrderedDict maintains the insertion order of items. Convert the list to an OrderedDict and then back to a list to preserve order.
from collections import OrderedDict list(OrderedDict.fromkeys(t)) # [1, 2, 3, 5, 6, 7, 8]
Using Dictionaries in Python 3.7
In Python 3.7 and later, regular dictionaries also maintain insertion order. Thus, you can use the following approach:
list(dict.fromkeys(t)) # [1, 2, 3, 5, 6, 7, 8]
Considerations
Note that all these methods require the items in the list to be hashable (i.e., immutable). If the items are not hashable (e.g., lists), a slower approach involving nested loop comparisons is necessary.
The above is the detailed content of How to Efficiently Remove Duplicates from a Python List While Maintaining Order?. For more information, please follow other related articles on the PHP Chinese website!