Drool 3: Fastest way to uniquify a list

With Python 3.6 and above, the order is preserved while inserting keys to a dictionary.

Hence, if you want to find unique elements in a list, here are 2 approaches that are not just fascinating, but are also the fastest.

Approach 1 - Preserving order of sequence

x = [1, 2, 3, 2, 3, 4, 5, 7, 6, 5]
print(list(dict.fromkeys(x)))
>>> [1, 2, 3, 4, 5, 7, 6]

Approach 2 - Without preserving the order of sequence

x = [1, 2, 3, 2, 3, 4, 5, 7, 6, 5]
print(list(set(x)))
>>> [1, 2, 3, 4, 5, 6, 7]

Other topics that might interest you.

  1. Find duplicate characters in a string
  2. An example based guide to working with Python Dictionaries

Follow @pydrools on Twitter

Don't forget to subscribe to the Pydrools Newsletter