Use a regex to pull the number from the string and cast to int:
import re
r = re.compile("\d+")
l = my_list= ['image101.jpg', 'image2.jpg', 'image1.jpg']
l.sort(key=lambda x: int(r.search(x).group()))
Or maybe use a more specific regex including the .
:
import re
r = re.compile("(\d+)\.")
l = my_list= ['image101.jpg', 'image2.jpg', 'image1.jpg']
l.sort(key=lambda x: int(r.search(x).group()))
Both give the same output for you example input:
['image1.jpg', 'image2.jpg', 'image101.jpg']
If you are sure of the extension you can use a very specific regex:
r = re.compile("(\d+)\.jpg$")
l.sort(key=lambda x: int(r.search(x).group(1)))