How do I get an empty array of any size in python?

user299648 picture user299648 · Mar 5, 2011 · Viewed 492.6k times · Source

I basically want a python equivalent of this in C:

int a[x];

but in python I declare an array like:

a = []

and the problem is I want to assign random slots with values like:

a[4] = 1

but I can't do that with python, since the array is empty.

Answer

Sven Marnach picture Sven Marnach · Mar 5, 2011

If by "array" you actually mean a Python list, you can use

a = [0] * 10

or

a = [None] * 10