Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to uniqify a list in Python

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]))
like image 910
Vojta Rylko Avatar asked Mar 26 '10 23:03

Vojta Rylko


People also ask

What is the fastest way to find an element in a list Python?

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.

How do you get unique words from a list in Python?

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.

Is Python list order guaranteed?

Yes, the order of elements in a python list is persistent.


3 Answers

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;-).

like image 124
Alex Martelli Avatar answered Oct 13 '22 20:10

Alex Martelli


set([a, b, c, a])

Leave it in that form if possible.

like image 32
Matt Joiner Avatar answered Oct 13 '22 20:10

Matt Joiner


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))
like image 5
pylang Avatar answered Oct 13 '22 19:10

pylang