Asked 7 years ago
23 Mar 2017
Views 1599
jessica

jessica posted

how to create multidimensional array in PYTHON ?

how to create multidimensional array in PYTHON ?
Mitul Dabhi

Mitul Dabhi
answered Nov 30 '-1 00:00

You can create multidimensional array with use of range in PYTHON.


A=[None] * 5
for i in range(3):
    A[i]=[None] * 2
print(A);


output ::
[[None, None], [None, None], [None, None], None, None]



A=[None] * 5
print(A);


create single dimensional array like this
[None, None, None, None, None]


for i in range(3):
    A[i]=[None] * 2


we are adding next dimension at each array element by for and range in Python
Rasi

Rasi
answered Nov 30 '-1 00:00


simply define array it as per need in PYTHON

A=[[1,2],[3,4]]
print(A);
ravi

ravi
answered Nov 30 '-1 00:00


w , h = 2 , 3
B =[[0]* w for i in range(h)]
print(B)


output ::
[[0, 0], [0, 0], [0, 0]]


B =[[0]* w for i in range(h)]

is short form of

D = [0] * w
for i in range(h):
    D[i]= [0] * 2

Post Answer