Fix Python – How do I get an empty list of any size in Python?

Question

Asked By – user299648

I basically want a Python equivalent of this Array 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 Python list is empty (of length 0).

Now we will see solution for issue: How do I get an empty list of any size in Python?


Answer

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

a = [0] * 10

or

a = [None] * 10

This question is answered By – Sven Marnach

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0