Knowledge Card - 2D List Structures

2D List Structures

  1. Definition: A 2D list is a list of lists.

    • Example: matrix = [[1, 2], [3, 4]] represents a 2x2 grid.
  2. Accessing Elements: Use two indices to access elements.

    • Example: matrix[0][1] returns 2.
  3. Common Uses: Represent grids, matrices, or tables.

    • Example: Tic-tac-toe board, image pixel data.
  4. Traversal: Use nested loops to traverse all elements.

    • Example: for row in matrix: for item in row: processes each element.