import re list_of_files = ["file1","file100","file4","file7"] def extract_number(f): s = re.findall("d+$",f) return (int(s[0]) if s else -1,f) print(max(list_of_files,key=extract_number))