ls=['ISSN1','94500','2424922X','','21693536','01464116','16879724','22042326','07419341','09272852','00015903','0324721X','']
ls = [i.zfill(8) for i in ls if i != ""]
ls
Output:
['000ISSN1',
'00094500',
'2424922X',
'21693536',
'01464116',
'16879724',
'22042326',
'07419341',
'09272852',
'00015903',
'0324721X']
However, this removed the empty element. I tried several other ways to keep the empty element:
method 1:
for i in range(len(ls)):
if ls[i]!="":
ls[i]=str(ls[i]).zfill(8)
else:
pass
ls
method 2:
def changes(ls):
for i in range(len(ls)):
if ls[i]!="":
ls[i]=str(ls[i]).zfill(8)
else:
pass
return ls
ls=changes(ls)
ls
Both methods return me with the desired output:
['000ISSN1',
'00094500',
'2424922X',
'',
'21693536',
'01464116',
'16879724',
'22042326',
'07419341',
'09272852',
'00015903',
'0324721X',
'']
I'm still wondering if there is a way to achieve the same result with list comprehension?
