title: What is a good pythonic way of finding duplicate objects?
body: <p>I frequently use <code>sorted</code> and <code>groupby</code> to find duplicates items in an iterable. Now I see it is unreliable:</p>
  
  <pre><code>from itertools import groupby
  data = 3 * ('x ',  (1,), u'x')
  duplicates = [k for k, g in groupby(sorted(data)) if len(list(g)) &gt; 1]
  print duplicates
  # [] printed - no duplicates found - like 9 unique values
  </code></pre>
  
  <p>The reason why the code above fails in Python 2.x is explained <a href="https://stackoverflow.com/questions/10238060/python-2-x-sorted-puzzlement">here</a>.</p>
  
  <p><strong>What is a reliable pythonic way of finding duplicates?</strong></p>
  
  <p>I looked for similar questions/answers on SO. The best of them is "<a href="https://stackoverflow.com/questions/685671/in-python-how-do-i-take-a-list-and-reduce-it-to-a-list-of-duplicates">In Python, how do I take a list and reduce it to a list of duplicates?</a>", but the accepted solution is not pythonic (it is procedural multiline for ... if ... add ... else ... add ... return result) and other solutions are unreliable (depends on unfulfilled transitivity of "&lt;" operator) or are slow (O n*n).</p>
  
  <p><strong>[EDIT]</strong> Closed. The accepted answer helped me to summarize conclusions in my answer below more general.</p>
  
  <p>I like to use builtin types to represent e.g. tree structures. This is why I am afraid of mix now.</p>