title: Python: How to sort items and remove duplicates in one pass using custom comparator?
body: <p>I have a list of tuples, which i need to sort by their second element and remove duplicates.</p>
  <p>Example input:</p>
  <pre><code>[
      (&quot;1&quot;, &quot;b&quot;),
      (&quot;2&quot;, &quot;e&quot;),
      (&quot;2&quot;, &quot;e&quot;),
      (&quot;3&quot;, &quot;d&quot;),
      (&quot;3&quot;, &quot;c&quot;),
      (&quot;4&quot;, &quot;a&quot;),
      (&quot;5&quot;, &quot;a&quot;),
  ]
  </code></pre>
  <p>expected output:</p>
  <pre><code>[
      (&quot;5&quot;, &quot;a&quot;),
      (&quot;4&quot;, &quot;a&quot;),
      (&quot;1&quot;, &quot;b&quot;),
      (&quot;3&quot;, &quot;c&quot;),
      (&quot;3&quot;, &quot;d&quot;),
      (&quot;2&quot;, &quot;e&quot;),
  ]
  </code></pre>
  <p>The common answer to this problem is</p>
  <pre><code>myList = sorted( set(myList), key = lambda x: x[1] )
  </code></pre>
  <p>But this doesn't seem like the best way to do it, because first constructs a set from a list, then it constructs back a list from a set (with random different order) and then it applies sorting algorithm to the list.</p>
  <p>In other languages that have better control over the containers and their implementations i would do this using ordered set, because that container will remove duplicates and sort the elements at the same time. I would like to write something like this.</p>
  <pre><code>myList = list( ordered_set( myList, key = lambda x: x[1] ) )
  </code></pre>
  <p>But in Python this seems like an overkill task. There is no <code>ordered_set</code> container in Python, or at least i haven't found one, and the <code>OrderedDict</code> added later in Python 3 does not seem to accept custom comparison lambdas.</p>
  <p>Does anyone know an easy way to do this?</p>