Python – list transformation

listpython

Does anyone knows what magic I have to use to change x list:

x = [1,2,3,4,5,11]

into y list?

y = ['01','02','03','04','05','11']

Thank you all in advance for helping me…

Best Solution

You can use a list comprehension (Python 2.6+):

y = ["{0:0>2}".format(v) for v in x]

Or for Python prior to 2.6:

y = ["%02d" % v for v in x]

Or for Python 3.6+ using f-strings:

y = [f'{v:02}' for v in x] 

Edit: Missed the fact that you wanted zero-padding...