pythonnatsort

Why does calling natsorted not modify my list?


from natsort import natsorted

filelist=[]
filelist=os.listdir("C:\\Users\\Amit\\Downloads\\Compressed\\trainigVid")
natsorted(filelist)
print filelist

I am getting following output

['1.avi', '10.avi', '11.avi', '12.avi', '13.avi', '14.avi', '15.avi',   '16.avi', '17.avi', '18.avi', '19.avi', '2.avi', '20.avi', '21.avi', '22.avi', '23.avi', '24.avi', '3.avi', '4.avi', '5.avi', '6.avi', '7.avi', '8.avi', '9.avi']

I want this list to be naturally sorted like

[1.avi, 2.avi, 3.avi.....]

I am stuck,please help


Solution

  • natsorted returns the newly sorted list, it does not modify the original list in place. This means you should use:

    filelist = natsorted(filelist)
    

    to get that return value.