Python Unique List (Remove Duplicates) - Python 3

June 5, 2019
items = [1, 3, 2, 3, 6, 3, 4, 5]

Sequence/Order is not important

unique_items = list(set(items))

Output

[1, 2, 3, 4, 5, 6]

NOTE: If possible, use sets in the first place to avoid list/sets conversion.

Order is to be maintained

from collections import OrderedDict
unique_items = list(OrderedDict.fromkeys(items))

NOTE: dict order is preserved since Python 3.6, but The order-preserving aspect of this new implementation is considered an implementation detail and should not be relied upon.

[1, 3, 2, 6, 4, 5]

or

def unique_list(seq):
    seen = set()
    seen_add = seen.add
    return [x for x in seq if not (x in seen or seen_add(x))]

NOTE: Source from Fastest way to uniqify a list in Python with discussion.

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.