---Advertisement---

Python 2D Arrays: Comprehensive Guide with Examples

By Bhavani

Updated On:

---Advertisement---
Python 2D Arrays

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:

  1. Creating a 2D Array in Python
  2. Accessing Values in a 2D Array
  3. Iterating Through a 2D Array
  4. Inserting Values into a 2D Array
  5. Updating Values in a 2D Array
  6. Deleting Values in a 2D Array
  7. Finding the Size of a 2D Array
  8. 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

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)

MethodDescriptionSyntaxExample
append()Adds an element at the end of the arrayarray.append(value)array.append(4)
clear()Removes all elements from the arrayarray.clear()array.clear()
copy()Copies the contents of one array to anotherarray1 = array.copy()print(array1)
count()Counts occurrences of a specified elementarray.count(value)print(array.count(3))
extend()Extends one array with elements of anotherarray.extend(array1)array.extend([4,5,6])
index()Returns the index of a specified elementarray.index(value)print(array.index(3))
insert()Inserts an element at a specified indexarray.insert(i, v)array.insert(2, 100)
pop()Removes an element at a specified indexarray.pop(index)array.pop(2)
remove()Removes a specified elementarray.remove(value)array.remove(2)
reverse()Reverses the elements in the arrayarray.reverse()array.reverse()

Python Array

Download Python

Leave a Comment

Index