Fastest way to uniqify a list in Python

前端 未结 5 1816
感动是毒
感动是毒 2020-12-31 00:08

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(s         


        
相关标签:
5条回答
  • 2020-12-31 00:47
    set([a, b, c, a])
    

    Leave it in that form if possible.

    0 讨论(0)
  • 2020-12-31 00:50

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

    0 讨论(0)
  • 2020-12-31 00:50

    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))
    
    0 讨论(0)
  • 2020-12-31 00:59

    Tim Peters wrote a classic general cookbook recipe for this problem back in 2001 (before sets were introduced). Comments by Alex Martelli, Raymond Hettinger et alia are informative and include updating to use sets etc.

    0 讨论(0)
  • 2020-12-31 01:00

    Check out this post with many different results. What you proposed above seems to be one of the fastest (and simplest ones)

    0 讨论(0)
提交回复
热议问题