Efficiently initialize 2D array of size n*m in Python 3?

Viewed 15344

I am using Python 3 and am trying to create 2D integer arrays of a known and fixed size. How can I do this?

The arrays I have in mind are the Python-provided array.array, although I am not averse to using a library like NumPy.

I know this can be done via lists, but I want to use an array since they are much more space efficient. Right now, I am initializing lists in this way, but I want to replace it with an array:

my_list = [[ 0 for _ in range(height) ] for _ in range(.width)]

For example, in C, I would write

int my_array[ width ][ height ];

How can I do this in Python

2 Answers
Related