Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add leading zeros to a list in Python

I have a list which has length 1000. E.g., x = [1.0, 2.0, 3.0, 4.0, …, 1000.0]. I would like to add 4 leading zero to above list. E.g. x_new = [0.0, 0.0, 0.0, 0.0, 1.0, 2.0, 3.0, …, 1000.0]

I don’t know how to add leading zeros to list. So, can you guys help me how to do it?

Thank you.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use list concatenation:

x_new = [0.0] * 4 + x

or

x_new = [0.0 for _ in range(4)] + x
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading