Python Unique List (Remove Duplicates) - Python 3

Jun 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 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.

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.