If by "array" you actually mean a Python list, you can use
a = [0] * 10
or
a = [None] * 10
Answer from Sven Marnach on Stack OverflowIf 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 want
a[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.
Implementing Array[T, N] in Python
Initialising an array of fixed size in Python - Stack Overflow
How to create an n-length array?
How should I initialize a numpy array of NaN values?
As a challenge, I want to implement an Array type in Python.
This type should have two generic parameters. The first parameter should be another type, and the second parameter should be a numeric value that indicates the size of the array. For example, I could write:
def sum(v: Array[int, 3]) -> int:
return v[0] + v[1] + v[2] # + v[3] will result in a type-check error
Unfortunately, using integral values as parameters for generic types is unsupported in mypy. There is an open and seemingly abandoned issue related to this problem: https://github.com/python/mypy/issues/3345.
However, it appears that by implementing __class_getitem__, I can generate a valid Array type at runtime, allowing me to write Python code with an Array type. Moreover, I believe that by implementing a mypy plugin, I can enable type-checking for code that uses this new Array type.
I haven't pursued this approach because I quickly realized that I have no idea how to implement functions that accept an array of any size, such as a size function that should return N for Array[T, N]. I'm wondering if there's a way to implement such a class as valid Python code that can be type-checked.
One approach I've considered is introducing a new function similar to TypeVar, called IntegerVar. By writing n = IntegerVar("n"), the code def foo(a: Array[int, n]) is valid Python code as long as I implement __class_getitem__ correctly. With enough effort, my mypy plugin might be able to correctly type-check this function. Unfortunately, this means I won't be able to use the actual value of n inside the code, which is a shame (e.g., for the size function mentioned above).
I'm sure I'm not the first person to think about this issue, so I'm wondering:
-
Am I missing something? Is it possible to implement an
Arrayin Python without jumping through hoops? -
If not, can anyone provide me with some direction on how I could implement this type while still allowing myself to write functions that are generic over the size of the array?
-
Are there any good tutorials or introductions for implementing
mypyplugins available?
You can use:
>>> lst = [None] * 5
>>> lst
[None, None, None, None, None]
Why don't these questions get answered with the obvious answer?
a = numpy.empty(n, dtype=object)
This creates an array of length n that can store objects. It can't be resized or appended to. In particular, it doesn't waste space by padding its length. This is the Python equivalent of Java's
Object[] a = new Object[n];
If you're really interested in performance and space and know that your array will only store certain numeric types then you can change the dtype argument to some other value like int. Then numpy will pack these elements directly into the array rather than making the array reference int objects.