2

I have a list:

my_list = ['0300', '0023', '0005', '000030']

I want to remove the preceding zeroes in each string element from the list. So I want strip the 0s from the left side of the string.

Output would be like this:

my_list = ['300', '23', '5', '30']

I've tried this:

for x in my_list:
    x = re.sub(r'^0+', "", x) 
    print my_list

But it doesn't seem to work. Please help!

1
  • 5
    l = [a.lstrip("0") for a in l] Commented Jun 13, 2014 at 14:03

2 Answers 2

6

You can use str.lstrip like this

print [item.lstrip('0') for item in l]
# ['300', '23', '5', '30']
Sign up to request clarification or add additional context in comments.

Comments

1

Try:

list = [str(int(el)) for el in list]

Edit: pick the other answer, I'd prefer that one too :-)

1 Comment

No it won't, I just tested it. However, the other solution is the better one :P. What you're thinking of is octal literals. Examples: int('0300') gives 300, int(0300) gives 192, int(0x300) gives 768. The quote marks make all the difference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.