In Python, a 2D array can be represented using a list of lists. Each inner list represents a row of the 2D array, and each element in the inner list represents a value in that row. Here’s an example of how to create a 2D array in Python:
# create a 2D array with 3 rows and 4 columns arr = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] # print the 2D array for row in arr: for element in row: print(element, end=' ') print()
This will output:
1 2 3 4 5 6 7 8 9 10 11 12
You can access elements of the 2D array using the row and column indices, like this:
# access the element at row 1, column 2 print(arr[1][2]) # output: 7
You can also modify the elements of the 2D array using the row and column indices:
# change the element at row 0, column 3 to 100 arr[0][3] = 100 # print the modified 2D array for row in arr: for element in row: print(element, end=' ') print()
This will output:
1 2 3 100 5 6 7 8 9 10 11 12
Traversing the element in 2D:
To traverse through all the elements of a 2D array in Python, you can use nested loops. Here’s an example of how to traverse through all the elements of a 2D array and print them:
# create a 2D array with 3 rows and 4 columns arr = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] # traverse through all the elements of the 2D array for i in range(len(arr)): for j in range(len(arr[i])): print(arr[i][j], end=' ') print()
This will output:
1 2 3 4 5 6 7 8 9 10 11 12
In the above example, the outer loop iterates over the rows of the 2D array and the inner loop iterates over the columns of each row. The len()
function is used to determine the length of each row, which is the same as the number of columns in the 2D array.
You can perform any operation on the elements of the 2D array within the nested loop. For example, you can calculate the sum of all the elements in the 2D array like this:
# calculate the sum of all elements in the 2D array sum = 0 for i in range(len(arr)): for j in range(len(arr[i])): sum += arr[i][j] print(sum) # output: 78
This will output the sum of all the elements in the 2D array, which is 78 in this case.
Insert elements in a 2D Array:
To insert an element into a 2D array in Python, you need to specify the row and column index where you want to insert the element. You can use the insert()
method to insert an element into a specific position in a list. Here’s an example of how to insert an element into a 2D array:
# create a 2D array with 3 rows and 4 columns arr = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] # insert the element 100 at row 1, column 2 arr[1].insert(2, 100) # print the modified 2D array for row in arr: for element in row: print(element, end=' ') print()
This will output:
1 2 3 4 5 6 100 7 8 9 10 11 12
In the above example, we used the insert()
method to insert the element 100 into row 1 and column 2 of the 2D array. The insert()
method takes two arguments: the index where you want to insert the element, and the element itself.
You can also append a new row to a 2D array by using the append()
method. Here’s an example of how to append a new row to a 2D array:
# create a 2D array with 3 rows and 4 columns arr = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] # append a new row to the 2D array arr.append([13, 14, 15, 16]) # print the modified 2D array for row in arr: for element in row: print(element, end=' ') print()
This will output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
In the above example, we used the append()
method to append a new row to the 2D array. The new row is specified as a list that contains the elements of the new row.
Delete values from a 2D array in Python:
To delete an element from a 2D array in Python, you need to specify the row and column index where the element is located. You can use the del
keyword or the pop()
method to delete an element from a list.
Here’s an example of how to delete an element from a 2D array using the del
keyword:
# create a 2D array with 3 rows and 4 columns arr = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] # delete the element at row 1, column 2 del arr[1][2] # print the modified 2D array for row in arr: for element in row: print(element, end=' ') print()
This will output:
1 2 3 4 5 6 8 9 10 11 12
In the above example, we used the del
keyword to delete the element at row 1 and column 2 of the 2D array.
You can also use the pop()
method to delete an element from a list. The pop()
method removes the element at the specified index and returns it. Here’s an example of how to delete an element from a 2D array using the pop()
method:
# create a 2D array with 3 rows and 4 columns arr = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] # delete the element at row 1, column 2 arr[1].pop(2) # print the modified 2D array for row in arr: for element in row: print(element, end=' ') print()
This will output the same result as the previous example:
1 2 3 4 5 6 8 9 10 11 12
In the above example, we used the pop()
method to delete the element at row 1 and column 2 of the 2D array.
Size of a 2D array:
The size of a 2D array in Python can be determined by using the len()
function. The len()
function returns the number of elements in a list or the number of rows in a 2D array.
To get the number of rows in a 2D array, you can use the len()
function on the outer list. To get the number of columns in a 2D array, you can use the len()
function on one of the inner lists.
Here’s an example of how to get the size of a 2D array:
# create a 2D array with 3 rows and 4 columns arr = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] # get the number of rows in the 2D array num_rows = len(arr) # get the number of columns in the 2D array num_cols = len(arr[0]) # print the size of the 2D array print("Number of rows:", num_rows) print("Number of columns:", num_cols)
This will output:
Number of rows: 3 Number of columns: 4
In the above example, we used the len()
function to get the number of rows and columns in the 2D array. We stored the number of rows in the variable num_rows
and the number of columns in the variable num_cols
, and then printed them out.