How to zfill after a certain value in a list

I have a list that looks like this

ls =

I want to zfill element in this list in order to sort my data.

Expected output:

ls =

zfill only allows me to add 0 on the left or on the right, How can I add the 0 only after the 9th value of the element of my list, i.e. the _ ?

>Solution :

You don’t even need to add zero and sort. If your goal is to sort the list use natsort directly.

from natsort import natsorted

new =natsorted(ls)

Gives #

['DATA2022_2.csv', 'DATA2022_3.csv', 'DATA2022_4.csv', 'DATA2022_5.csv', 'DATA2022_6.csv', 'DATA2022_7.csv', 'DATA2022_8.csv', 'DATA2022_9.csv', 'DATA2022_10.csv']

Leave a Reply