title: Find file in directory with the max number given a set of different file names
body: <p><strong>Problem Description</strong></p>
  <p>I have a list of files <code>[&quot;FA_1&quot;,&quot;FA_2&quot;,&quot;FB_1&quot;,&quot;FB_2&quot;,&quot;FC_1&quot;,&quot;FC_2&quot;]</code>. That list has <code>3</code> different file names <code>FA</code>, <code>FB</code> and <code>FC</code>. For each of <code>FA</code>, <code>FB</code> and <code>FC</code>, I am trying to retrieve the one with the <code>max number</code>.  The following script that I coded does that. But it's so complicated and ugly.</p>
  <p><strong>Is there a way to make it simpler?</strong></p>
  <p>A similar question was asked in <a href="https://stackoverflow.com/q/43074685/2721822">Find file in directory with the highest number in the filename</a>. But, they are only using the same file name.</p>
  <hr />
  <pre><code>#!/usr/bin/env python
  
  import sys
  import os
  from collections import defaultdict
  
  def load_newest_files():
      # Retrieve all files for the component in question
      list_of_files = defaultdict(list)
      new_list_of_files = []
      files = [&quot;FA_1&quot;,&quot;FA_2&quot;,&quot;FB_1&quot;,&quot;FB_2&quot;,&quot;FC_1&quot;,&quot;FC_2&quot;]
  
      # Split files and take the filename without the id.
      # The files are not separated in bucket of FA, FB and FC
      # I can now retrieve the file with the max number and put
      # it in a list
      for file in files:
          list_of_files[file.split(&quot;_&quot;)[0]].append(file)
  
      for key,value in list_of_files.items():
          new_list_of_files.append(max(value))
      
      print(new_list_of_files)
  
  
  def main():
      load_newest_files()
  
  if __name__ == &quot;__main__&quot;:
      main()
  </code></pre>