2D Arrays
A 2D array is a data structure that stores elements in a grid or matrix of rows and columns. It
consists of multiple rows and columns, where each element is uniquely identified by its row
and column values. 2D arrays are commonly used in programming to represent images,
screens and game boards.
Syntax:
2d_array = [[col1,col2,col3], [col1,col2,col3], [col1,col2,col3], [col1,col2,col3]]
We are to specify an identifier for the 2d array and assign the elements by separating each
row with square brackets “ [ ] ” and each element in column by a comma “,”
Create an array with 4 rows and 3 columns. The whole 2d array is to be enclosed in square
brackets as well and each row is to be separated by a comma too.
Example:
Create a 2d array with 5 rows and 3 columns, one in integer and one in string data type
Code:
array2d_1=[[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0]]
array2d_2=[["a","b","c"],["d","e","f"],["g","h","i"],["j","k","l"],["m","n"
,"o"]]
print(array2d_1)
print(array2d_2)
Output:
Practice Question:
Create a 2d array with 4 rows and 7 columns with integer data type
Solution:
array2d=[[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0]]
How to access individual element in 2D Array
If you are to access an individual element in a 2D array, just like in 1D array, you are to
specify the index of the element but the only difference in 2D is that you need to specify the
index of row and column, enclosed in square brackets for rows and columns separately.
Syntax:
Array2D[RowIndex][ColumnIndex]
All elements of 2D arrays may also be accessed using loops, however, you are to use the
nested loops to access all elements, in which the outer loop will be cycling through the rows
and the inner loop will cycle through the columns.
Practice Question:
Create a 2D array of 4 rows and 7 columns of integer data type and initial elements of the
whole 2D array to be zero. Assign all elements of the array with 1 and take the output of the
updated 2D array.
Solution:
array2d=[[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0]]
print(array2d)
for row in range(4):
for col in range(7):
array2d[row][col]=1
print(array2d)
Output:
Practice Question:
Create a 2D array with 2 rows and 3 columns of string data type with the following
values/elements
Row 1= Ibrahim, Taha, Nauman
Row 2 = Fatima, Saba, Abeeha
Output the message “Student exists” if Saba is given as an input.
Solution:
std_array=[["Ibrahim","Taha","Nauman"],["Fatima","Saba","Abeeha"]]
for row in range(2):
for col in range(3):
if std_array[row][col]=="Saba":
print("Student exists")
Output:
Note: if you add a value false for the selection statement then we get an output for each
element of the array whether the student exists or not in that specific index
Code:
std_array=[["Ibrahim","Taha","Nauman"],["Fatima","Saba","Abeeha"]]
for row in range(2):
for col in range(3):
if std_array[row][col]=="Saba":
print("Student exists")
else:
print("Student does not exists")
Output:
Creating arrays using Loops
1D Array:
Code:
BlankArray=[""]*20
The code above will create a 1D array as string with 20 empty string elements
BlankArray=[""]*20
print(BlankArray)
Output:
2D Array:
Code:
Blank2D = [[""] * 10 for i in range(5)]
This line of code creates a 2D array with 5 rows and 10 columns with the name Blank2D
containing Empty Strings
Blank2D = [[""] * 10 for i in range(5)]
print(Blank2D)
Output: