
Python 2D Arrays
What is a 2D Array?
Python 2D Arrays: A 2D array (or two-dimensional array) is an array inside another array. It is a powerful data structure used to store data in rows and columns. Each element is accessed using its unique index.
Python 2D Arrays Table of Contents:
- Creating a 2D Array in Python
- Accessing Values in a 2D Array
- Iterating Through a 2D Array
- Inserting Values into a 2D Array
- Updating Values in a 2D Array
- Deleting Values in a 2D Array
- Finding the Size of a 2D Array
- Important Array Methods
1. Creating a 2D Array in Python
Python 2D Arrays Syntax:
array = [[r1, r2, r3, …, rn], [c1, c2, c3, …, cn]]
Where:
- r represents rows
- c represents columns
Python 2D Arrays Example:
array = [[23, 45, 43, 23, 45],
[45, 67, 54, 32, 45],
[89, 90, 87, 65, 44],
[23, 45, 67, 32, 10]]
print(array)
Output:
[[23, 45, 43, 23, 45], [45, 67, 54, 32, 45], [89, 90, 87, 65, 44], [23, 45, 67, 32, 10]]
2. Accessing Values in a 2D Array
Syntax:
- array[row_index] ➔ Accesses an entire row
- array[row_index][column_index] ➔ Accesses a specific element
Example:
print(array[0]) # First row
print(array[2]) # Third row
print(array[0][2]) # First row, third element
print(array[2][3]) # Third row, fourth element
Output:
[23, 45, 43, 23, 45]
[89, 90, 87, 65, 44]
43
65
3. Iterating Through a 2D Array
Syntax:
for row in array:
for column in row:
print(column, end=” “)
print()
Output:
23 45 43 23 45
45 67 54 32 45
89 90 87 65 44
23 45 67 32 10
4. Inserting Values into a 2D Array
Syntax:
array.insert(index, [values])
Example:
array.insert(2, [1, 2, 3, 4, 5])
print(array)
Output:
[[23, 45, 43, 23, 45], [45, 67, 54, 32, 45], [1, 2, 3, 4, 5], [89, 90, 87, 65, 44], [23, 45, 67, 32, 10]]
5. Updating Values in a 2D Array
Syntax:
- Update entire row: array[row_index] = [values]
- Update specific element: array[row_index][column_index] = value
Example:
array[2] = [0, 3, 5, 6, 7] # Update third row
array[0][2] = 100 # Update first row’s third element
print(array)
Output:
[[23, 45, 100, 23, 45], [45, 67, 54, 32, 45], [0, 3, 5, 6, 7], [23, 45, 67, 32, 10]]
6. Deleting Values from a 2D Array
Syntax:
del array[index]
Example:
del array[2] # Delete third row
print(array)
Output:
[[23, 45, 100, 23, 45], [45, 67, 54, 32, 45], [23, 45, 67, 32, 10]]
7. Finding the Size of a 2D Array
Syntax:
len(array)
Example:
print(len(array))
Output:
4
8. Important Array Methods(Python 2D Arrays)
Method | Description | Syntax | Example |
append() | Adds an element at the end of the array | array.append(value) | array.append(4) |
clear() | Removes all elements from the array | array.clear() | array.clear() |
copy() | Copies the contents of one array to another | array1 = array.copy() | print(array1) |
count() | Counts occurrences of a specified element | array.count(value) | print(array.count(3)) |
extend() | Extends one array with elements of another | array.extend(array1) | array.extend([4,5,6]) |
index() | Returns the index of a specified element | array.index(value) | print(array.index(3)) |
insert() | Inserts an element at a specified index | array.insert(i, v) | array.insert(2, 100) |
pop() | Removes an element at a specified index | array.pop(index) | array.pop(2) |
remove() | Removes a specified element | array.remove(value) | array.remove(2) |
reverse() | Reverses the elements in the array | array.reverse() | array.reverse() |