🚀 Supercharge your YouTube channel's growth with AI.
Try YTGrowAI FreePython Two Dimensional Array: Create and Use

Working with row-and-column values in Python calls for a structure that keeps each row together. A list of lists stores rows in an outer list and values in inner lists.
This article explains how to build independent rows and add a row to the grid.
TL;DR
A Python two-dimensional array is commonly a list whose items are rows, with each cell selected by two indexes.
- Use grid[row][column] to read or change a cell.
- Build rows separately with a comprehension to avoid shared-row references.
- Use NumPy when you need numerical array operations and a shape attribute.
What Is a Two-Dimensional Array in Python?
For ordinary Python code, a two-dimensional structure is often a list of lists: an outer list holds rows, and each row is an inner list of values. The standard library also has array.array, a typed one-dimensional sequence, while NumPy provides multidimensional ndarray objects.
In a nested list, grid[1][2] means row index 1 and then column index 2. Both indexes start at zero. Python lists can have different row lengths, so the structure does not enforce a rectangular table.
How to Create and Use a Two-Dimensional Array
A nested list gives you row-based storage with built-in list operations. This short example creates a grid, reads and changes a cell, then adds a row.
Step 1: Create rows with values
Write each row as a list inside the outer list. The values can be numbers, strings or other Python objects.
grid = [[10, 12, 14], [0, 1, 2]]
print(grid)
The outer list has two rows, each with three values.
Step 2: Make an empty grid safely
Use a list comprehension when you need several independent rows. The repeated-list form [[]] * 3 reuses the same inner list three times, so changing one row appears in all three.
rows, columns = 3, 4
grid = [[0 for _ in range(columns)] for _ in range(rows)]
Each comprehension pass makes a new row. For a populated table, replace zero with the initial value your task needs.
Step 3: Read and update a cell
Chain indexes to move from the outer row to a value inside it. Assign to the same expression to update that cell.
grid = [[10, 12, 14], [0, 1, 2]]
print(grid[1][2])
grid[1][2] = 99
print(grid[1])
The first expression reads the third value in the second row. The assignment changes that cell. An index outside the row’s bounds raises IndexError.
Step 4: Add rows and measure the grid
Call append() with a list to add one complete row. Calling extend() with a row instead adds that row’s individual values to the outer list.
grid = [[10, 12], [0, 1]]
grid.append([5, 6])
print(len(grid), len(grid[0]))
The captured run printed the selected value, the updated row and the full grid after appending:
The run’s output was:
2
[0, 1, 99]
[[10, 12, 14], [0, 1, 99], [5, 6, 7]]

len(grid) counts rows, and len(grid[0]) counts the first row’s columns. For uneven rows, check each row separately rather than treating the first row’s length as universal.
Step 5: Read rows from input
For line-based numeric input, split each row and convert its tokens before appending it. Validate the expected number of rows and columns when the input format specifies them.
row_count = int(input())
grid = []
for _ in range(row_count):
grid.append([int(value) for value in input().split()])
print(grid)
Each input line becomes one row. Non-integer tokens raise ValueError, and blank lines create empty rows unless you reject them.
Edge Cases and Common Mistakes
Nested lists are flexible, which means your code must decide whether all rows need equal lengths. Check row shape before indexing when data can be incomplete or irregular.
| Case | Symptom | Fix |
|---|---|---|
| Repeated inner list | Editing one row changes several rows | Use a comprehension to create independent lists |
| Index beyond row length | IndexError | Check the row length before accessing the column |
| Different row lengths | A cell position is missing in some rows | Validate or handle each row’s length |
| Flattening with extend() | Values join the outer list instead of forming a row | Append the row list or wrap rows in an outer list |
For numerical work, NumPy’s np.array() converts a list of lists into an ndarray. Its shape reports the size along each dimension, and its operations are designed for array calculations.
See the Python array examples for standard-library array operations and the NumPy regression guide for ndarray use.
Conclusion
Use a list of lists for straightforward row-and-column data, create each row independently, and use two indexes to reach a cell. Choose NumPy when multidimensional numerical operations and shape-aware processing are part of the task.
The Python list documentation explains list operations, and the NumPy array creation guide covers ndarray construction.
FAQ
These answers address common questions about declaring and accessing two-dimensional data.
How do you declare a two-dimensional array in Python?
Create an outer list containing one inner list per row, such as [[1, 2], [3, 4]]. For independent blank rows, use a list comprehension.
How do you access an element in a two-dimensional list?
Use two indexes: grid[row_index][column_index]. Both indexes start at zero.
How do you add a row to a two-dimensional list?
Call grid.append(row_list) to add one complete row. extend(row_list) adds its values to the outer list instead.


