How to Initiate 2D Array in Python
 List Comprehension Method to Initiate a 2D Array

Nested
range
Method to Initiate a 2D Array 
numpy
Method to Initiate a 2D Array
This tutorial guide will introduce different methods to initiate a 2D array in Python. We will make a 3x5
2D array in the following examples.
List Comprehension Method to Initiate a 2D Array
>>> column, row = 3, 5
>>> array2D = [[0 for _ in range(row)] for _ in range(column)]
>>> array2D
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
This nested list comprehension method creates 2D array with the initial value as 0
. Of course, you could change the initial value to any value you need to assign in your application.
Nested range
Method to Initiate a 2D Array
If you don’t care about the initial value in the 2D array, the value 0
could be even eliminated.
In Python 2.x
>>> column, row = 3, 5
>>> A = [range(row) for _ in range(column)]
>>> A
[[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]
In Python 3.x
>>> column, row = 3, 5
>>> A = [range(row) for _ in range(column)]
>>> A
[range(0, 5), range(0, 5), range(0, 5)]
We couldn’t simply use range(x)
to initiate 2D array in Python 3.x because range
returns an object containing a sequence of integers in Python 3.x, but not a list of integers as in Python 2.x.
range
in Python 3.x is more similar to xrange
in Python 2.x. range
object in Python 3.x is immutable, therefore, you don’t assign items to its elements.
If you need item assignment, you need to convert the range
to list
object.
>>> A = [list(range(row)) for _ in range(column)]
>>> A
[[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]
[0] * N
Method to Initiate a 2D Array
One Pythonic way to initiate a 2D array could be
>>> column, row = 3, 5
>>> A = [[0]*row for _ in range(column)]
>>> A
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
Although we should be cautious when we use list multiplication because it simply creates a sequence with multiple times referred to one same object, we are relieved to use [0]*n
here because data object 0
is immutable so that we will never encounter problems even with references to the same immutable object.
numpy
Method to Initiate a 2D Array
Besides the native Python array, numpy
should be the best option to create a 2D array, or to be more precise, a matrix.
You could create a matrix filled with zeros with numpy.zeros
.
>>> import numpy as np
>>> column, row = 3, 5
>>> np.zeros(column, row)
array([[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]])
Or initiate a matrix filled with ones
with numpy.ones
>>> import numpy as np
>>> column, row = 3, 5
>>> np.ones((column, row))
array([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]])
You could even create a new array without initializing entries with numpy.empty
>>> import numpy as np
>>> column, row = 3, 5
>>> np.empty((5,5))
array([[6.23042070e307, 4.67296746e307, 1.69121096e306,
1.33511562e306, 1.89146896e307],
[7.56571288e307, 3.11525958e307, 1.24610723e306,
1.37962320e306, 1.29060871e306],
[2.22518251e306, 1.33511969e306, 1.78022342e306,
1.05700345e307, 1.11261027e306],
[1.11261502e306, 1.42410839e306, 7.56597770e307,
6.23059726e307, 1.42419530e306],
[7.56599128e307, 1.78022206e306, 8.34451503e308,
2.22507386e306, 7.20705877e+159]])
It is a better solution if you want to create the empty array first and then assign the element values later. But be aware that random values are in the array so that it could be risky if you access the array by indexing before value of the corresponding index has been assigned.