Knowledge Card - 2D List Structures
2D List Structures
-
Definition: A 2D list is a list of lists.
- Example:
matrix = [[1, 2], [3, 4]]
represents a 2x2 grid.
- Example:
-
Accessing Elements: Use two indices to access elements.
- Example:
matrix[0][1]
returns2
.
- Example:
-
Common Uses: Represent grids, matrices, or tables.
- Example: Tic-tac-toe board, image pixel data.
-
Traversal: Use nested loops to traverse all elements.
- Example:
for row in matrix: for item in row:
processes each element.
- Example: