How to Define a 2 Dimensional Array in Python?

python

To define a 2-dimensional array in Python, you can use a list of lists where each inner list represents a row in the array. Here is an example:

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

You can access individual elements in the array by specifying the row and column indices:

# Access element in row 1, column 2 (value 3)
elem = my_array[0][2]

You can also modify elements in the array:

# Change element in row 2, column 3 from 8 to 99
my_array[1][2] = 99

Latest Questions

python How to Fix ""zsh: command not found: python" Error on MacOS X? python How to Fix "xlrd.biffh.XLRDError: Excel xlsx file; not supported" Error in Pandas? python How to Remove All Whitespace From a Python String?