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.

>Solution :

You can use list concatenation:

x_new = [0.0] * 4 + x

or

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

Leave a Reply