Fastest way to uniqify a list in Python without preserving order? I saw many complicated solutions on the Internet - could they be faster than simply:
list(set([a,b,c,a]))
To find an element in the list, use the Python list index() method, The index() is an inbuilt Python method that searches for an item in the list and returns its index. The index() method finds the given element in the list and returns its position.
Using Python's import numpy, the unique elements in the array are also obtained. In the first step convert the list to x=numpy. array(list) and then use numpy. unique(x) function to get the unique values from the list.
Yes, the order of elements in a python list is persistent.
Going to a set only works for lists such that all their items are hashable -- so e.g. in your example if c = []
, the code you give will raise an exception. For non-hashable, but comparable items, sorting the list, then using itertools.groupby
to extract the unique items from it, is the best available solution (O(N log N)). If items are neither all hashable, nor all comparable, your only "last ditch" solution is O(N squared)
.
You can code a function to "uniquify" any list that uses the best available approach by trying each approach in order, with a try
/except
around the first and second (and a return
of the result either at the end of the try
clause, or, elegantly, in an else
clause of the try
statement;-).
set([a, b, c, a])
Leave it in that form if possible.
This updated post by Peter Bengtsson suggests two of the fastest ways to make a list of unique items in Python 3.6+ are:
# Unordered (hashable items)
list(set(seq))
# Order preserving
list(dict.fromkeys(seq))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With