title: Python/Itertools: Get latest file by name
body: <p>I have a list of filenames in a directory and I'd like to keep only the latest versions. The list looks like: </p>
  
  <p><code>['file1-v1.csv', 'file1-v2.csv', 'file2-v1.txt', ...]</code>. </p>
  
  <p>I'd like to only keep the newest csv files as per the version (part after <code>-</code> in the filename) and the txt files. </p>
  
  <p>The output would be <code>[''file1-v2.csv', 'file2-v1.txt', ...]</code></p>
  
  <p>I have a solution that requires the use of sets but I'm looking for a easy pythonic way to do this. Potentially using <code>itertools</code> and <code>groupby</code></p>
  
  <p><strong>Update: Solution so far</strong></p>
  
  <p>I've been able to do some preliminary work to get a list like</p>
  
  <pre><code>lst = [('file1', 'csv', 'v1','&lt;some data&gt;'), ('file2', 'csv', 'v2','&lt;some data&gt;'), ...]
  </code></pre>
  
  <p>I'd like to group by elements at index <code>0</code> and <code>1</code> but provide only the tuple with the maximum index <code>2</code>.</p>
  
  <p>It may be something like the below:</p>
  
  <pre><code>files = list(item for key, group in itertools.groupby(files, lambda x: x[0:2]) for item in group)
  # Maximum over 3rd index element in each tuple does not work
  files = max(files, key=operator.itemgetter(2))
  </code></pre>
  
  <p>Also, I feel like the below should work but it does not select the maximum properly</p>
  
  <pre><code>[max(items, key=operator.itemgetter(2)) for key, items in itertools.groupby(files, key=operator.itemgetter(0, 1))]
  </code></pre>