Skip to content

Python 2D array

  • by

In Python, a 2D array is typically represented as a list of lists. Each inner list represents a row in the 2D array, and the elements within the inner list represent the columns. Here’s an example of creating and accessing elements in a 2D array:

A 2D array is a data structure that represents a matrix or a table-like structure with rows and columns. It is also known as a two-dimensional array or a matrix.

array_2d = [[1, 2, 3],
            [4, 5, 6],
            [7, 8, 9]]

You can create and work with a 2D array using various syntax options. Here are a few commonly used syntaxes:

List of Lists:

array_2d = [[1, 2, 3],
            [4, 5, 6],
            [7, 8, 9]]

Nested List Comprehension:

rows = 3
cols = 3
array_2d = [[0 for _ in range(cols)] for _ in range(rows)]

NumPy Library:

import numpy as np

array_2d = np.array([[1, 2, 3],
                     [4, 5, 6],
                     [7, 8, 9]])

Here are some common operations you can perform on a 2D array:

In Python, you can perform various operations on a 2D array to manipulate its elements, perform calculations, or extract specific information.

Accessing Elements:

  • Access a specific element: array_2d[row_index][col_index]
  • Access a row: array_2d[row_index]
  • Access a column: [row[col_index] for row in array_2d]

Modifying Elements:

  • Assign a new value to an element: array_2d[row_index][col_index] = new_value

Traversing the Array: Iterate over each element using nested loops:

for row in array_2d:
    for element in row:
        # Perform operations on each element
        print(element)

Performing Calculations:

  • Sum of all elements: sum([sum(row) for row in array_2d])
  • Minimum element: min(min(row) for row in array_2d)
  • Maximum element: max(max(row) for row in array_2d)
  • Average of all elements: sum(sum(row) for row in array_2d) / (len(array_2d) * len(array_2d[0]))

Transposing the Array: Convert rows into columns and columns into rows:

transposed_array = [[array_2d[j][i] for j in range(len(array_2d))] for i in range(len(array_2d[0]))]

Finding Specific Elements:

  • Check if an element exists: element in [item for sublist in array_2d for item in sublist]
  • Find the indices of an element: [(i, j) for i, row in enumerate(array_2d) for j, elem in enumerate(row) if elem == target]

Python 2D array examples

Here’s an example that demonstrates creating a 2D array, accessing its elements, and modifying them:

# Create a 2D array with 3 rows and 4 columns
array_2d = [[1, 2, 3, 4],
            [5, 6, 7, 8],
            [9, 10, 11, 12]]

# Accessing elements in the 2D array
print(array_2d[0][0])  
print(array_2d[1][2]) 
print(array_2d[2][3]) 

# Modifying elements in the 2D array
array_2d[0][1] = 20
array_2d[2][2] = 30

for row in array_2d:
    print(row)

Output:

Python 2D array

Here are a few examples of working with 2D arrays in Python:

Creating and accessing elements in a 2D array

# Create a 2D array with 3 rows and 4 columns
array_2d = [[1, 2, 3, 4],
            [5, 6, 7, 8],
            [9, 10, 11, 12]]

# Accessing elements in the 2D array
print(array_2d[0][0])  # Output: 1
print(array_2d[1][2])  # Output: 7
print(array_2d[2][3])  # Output: 12

Modifying elements in a 2D array

# Create a 2D array
array_2d = [[1, 2, 3],
            [4, 5, 6],
            [7, 8, 9]]

# Modifying elements in the 2D array
array_2d[0][1] = 10
array_2d[2][2] = 20

# Print the modified array
for row in array_2d:
    print(row)

Nested List Comprehension to create a 2D array

# Create a 2D array with 3 rows and 3 columns, initialized to 0
rows = 3
cols = 3
array_2d = [[0 for _ in range(cols)] for _ in range(rows)]

# Print the array
for row in array_2d:
    print(row)

Comment if you have any doubts or suggestions on this Python Array topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *