asd
0
Q:

create a 2d array in python

# Using above second method to create a  
# 2D array 
rows, cols = (5, 5) 
arr = [[0 for i in range(cols)] for j in range(rows)] 
print(arr) 
0
def build_matrix(rows, cols):
    matrix = []

    for r in range(0, rows):
        matrix.append([0 for c in range(0, cols)])

    return matrix

if __name__ == '__main__':
    build_matrix(6, 10)
5
o=[]
for i in range(0,rows):
    x=[]
    for j in range(0,cols):
        x.append(0)
    o.append(x)
#if you use [[0]*cols]*rows all rows will become the same list
#so editing in one row will edit all rows
1
from array import *

T = [[11, 12, 5, 2], [15, 6,10], [10, 8, 12, 5], [12,15,8,6]]
for r in T:
    for c in r:
        print(c,end = " ")
    print()
2
# Using above first method to create a  
# 2D array 
rows, cols = (5, 5) 
arr = [[0]*cols]*rows 
print(arr) 
4

New to Communities?

Join the community