Multidimensional Lists

Carson West

Lists

Multidimensional Lists

Multidimensional lists in Python are lists within lists, creating a structure that can represent matrices, tables, or other higher-dimensional data. The number of dimensions is limited only by memory and practicality.

Example:

# A 2D list (matrix)
matrix = [
    1, 2, 3,
    4, 5, 6,
    7, 8, 9]]
]]

# Accessing elements:
print(matrix[0]][0]])  # Output: 1 (first row, first column)
print(matrix12)  # Output: 6 (second row, third column)


# A 3D list (e.g., a cube of data)
cube = [
    1, 2, 3, 4,
    5, 6, [7, 8](./../7,-8/),
    [9, 10](./../9,-10/), [11, 12]]
]]

print(cube1[0]]1) #Output: 6

Common Use Cases:

Important Considerations:

Example using List Comprehension to create a 2D list:

rows = 3
cols = 4
matrix = [i * cols + j for j in range(cols)](./../i-*-cols-+-j-for-j-in-range(cols)/) for i in range(rows)]]
print(matrix)

Example of accessing elements using nested loops:

for row in matrix:
    for element in row:
        print(element, end=" ")
    print()