Table of contents | |
What is a 2D Array? | |
Creating a 2D Array | |
Accessing Elements in a 2D Array | |
Iterating Over a 2D Array | |
Modifying Elements in a 2D Array | |
Sample Problems |
In Python, a 2D array, also known as a two-dimensional array, is a data structure that represents a matrix or a table-like structure consisting of rows and columns. Unlike a 1D array, which is a linear sequence of elements, a 2D array allows you to organize and access data in a grid-like format.
Each element in a 2D array is identified by its row and column indices. Rows are numbered from top to bottom, and columns are numbered from left to right.
In Python, you can create a 2D array using nested lists. Each element in the outer list represents a row, and each element within the row represents a column value.
Here's an example of creating a 2D array with three rows and three columns:
# Creating a 2D array
my_array = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
In the above example, 'my_array' is a 2D array with three rows and three columns. It can be visualized as follows:
1 2 3
4 5 6
7 8 9
To access a specific element in a 2D array, you can use the row and column indices.
For example, to access the element in the second row and third column of the 'my_array':
# Accessing an element in a 2D array
element = my_array[1][2]
print(element) # Output: 6
In the above code, 'my_array[1][2]' returns the value '6', which is the element in the second row and third column.
You can use nested loops to iterate over all the elements in a 2D array.
# Iterating over a 2D array
for row in my_array:
for element in row:
print(element, end=' ')
print()
Output:
1 2 3
4 5 6
7 8 9
In the above code, the outer loop iterates over each row, and the inner loop iterates over each element within the row. By printing each element and adding a newline character after each row, we can display the entire 2D array.
You can modify elements in a 2D array by assigning a new value to a specific element using its row and column indices.
# Modifying an element in a 2D array
my_array[1][2] = 10
print(my_array)
Output:
[[1, 2, 3],
[4, 5, 10],
[7, 8, 9]]
In the above code, 'my_array[1][2]' is modified from '6' to '10'. The resulting array is:
1 2 3
4 5 10
7 8 9
Here are a few sample problems that you can solve using 2D arrays:
Problem 1: Finding the Sum of all Elements in a 2D Array
Given a 2D array, write a program to find the sum of all its elements.
# Problem 1: Finding the Sum of all Elements in a 2D Array
def find_sum_2d_array(arr):
total_sum = 0
for row in arr:
for element in row:
total_sum += element
return total_sum
# Test the function
my_array = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
print(find_sum_2d_array(my_array)) # Output: 45
Problem 2: Transposing a 2D Array
Given a 2D array, write a program to transpose it, i.e., convert its rows into columns and columns into rows.
# Problem 2: Transposing a 2D Array
def transpose_2d_array(arr):
transposed_array = []
for col_index in range(len(arr[0])):
transposed_row = []
for row in arr:
transposed_row.append(row[col_index])
transposed_array.append(transposed_row)
return transposed_array
# Test the function
my_array = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
transposed = transpose_2d_array(my_array)
for row in transposed:
print(row)
Output:
[1, 4, 7]
[2, 5, 8]
[3, 6, 9]
In the above code, the 'transpose_2d_array' function transposes the given array by swapping rows with columns.
You've learned the basics of 2D arrays in Python. You now know how to create, access, iterate over, and modify elements in a 2D array. Additionally, you've solved a couple of sample problems to solidify your understanding.
49 videos|38 docs|18 tests
|
|
Explore Courses for Software Development exam
|