2d arrays python with n rows and m columns code example

Example 1: how to input 2-d array in python

matrix = [input().split() for i in range(no_of_rows)] # if only row is given and the number of coloumn has to be decide by user
matrix= [[input() for j in range(no_of_cols)] for i in range(no_of_rows)] # if both row and coloumn has been taken as input from user

Example 2: two dimensional array python

# Creates a list containing 5 lists, each of 8 items, all set to 0
w, h = 8, 5;
Matrix = [[0 for y in range(h)] for x in range(w)] 

Matrix[0][0] = 1
Matrix[0][6] = 3 # error! range... 
Matrix[6][0] = 3 # valid

Tags:

Cpp Example