Home > Backend Development > Python Tutorial > How to Efficiently Remove Duplicates from a Python List While Maintaining Order?

How to Efficiently Remove Duplicates from a Python List While Maintaining Order?

DDD
Release: 2024-12-20 12:42:19
Original
586 people have browsed it

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]
Copy after login

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]
Copy after login

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]
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template