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

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


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

a = [0] * 10

or

a = [None] * 10


You can't do exactly what you want in Python (if I read you correctly). You need to put values in for each element of the list (or as you called it, array).

But, try this:

a = [0 for x in range(N)]  # N = size of list you wanta[i] = 5  # as long as i < N, you're okay

For lists of other types, use something besides 0. None is often a good choice as well.


You can use numpy:

import numpy as np

Example from Empty Array:

np.empty([2, 2])array([[ -9.74499359e+001,   6.69583040e-309],       [  2.13182611e-314,   3.06959433e-309]])