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 OrderedDictunique_items = list(OrderedDict.fromkeys(items))
or
unique_items = list(dict.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
. Since Python 3.7, dict order is guaranteed.
[1, 3, 2, 6, 4, 5]
or
def unique_list(items): seen = set() seen_add = seen.add return [x for x in items if not (x in seen or seen_add(x))]
NOTE: Source from Fastest way to uniqify a list in Python with discussion.
Objects
For list of objects, refer to Python Unique List (Remove Duplicates) of Object Attributes.