title: Filtering list of tuples based on condition
body: <p>For a given list of tuples, if multiple tuples in the list have the first element of tuple the same - among them select only the tuple with the maximum last element.</p>
  <p>For example:</p>
  <pre><code>sample_list = [(5,16,2),(5,10,3),(5,8,1),(21,24,1)]
  </code></pre>
  <p>In the <code>sample_list</code> above since the first 3 tuples has the similar first element <code>5</code> in this case among them only the 2nd tuple should be retained since it has the max last element =&gt; <code>3</code>.</p>
  <p>Expected op:</p>
  <pre><code>op = [(5,10,3),(21,24,1)]
  </code></pre>
  <p>Code:</p>
  <pre><code>op = []
  for m in range(len(sample_list)):
      li = [sample_list[m]]
      for n in range(len(sample_list)):
          if(sample_list[m][0] == sample_list[n][0]
             and sample_list[m][2] != sample_list[n][2]):
              li.append(sample_list[n])
      op.append(sorted(li,key=lambda dd:dd[2],reverse=True)[0])
  
  print (list(set(op)))
  </code></pre>
  <p>This works. But it is very slow for long list. Is there a more pythonic or efficient way to do this?</p>