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:
- Representing matrices for mathematical operations.
- Storing tabular data (like spreadsheets).
- Implementing game boards or other grid-based structures.
Important Considerations:
- Memory Efficiency: Multidimensional lists can consume significant memory, especially with large dimensions. Consider using NumPy arrays for better performance and memory management with large datasets. NumPy Arrays
- Nested Loops: Accessing and manipulating elements often requires nested loops.
- List Comprehension: List comprehensions can be used to create and manipulate multidimensional lists concisely. List Comprehensions
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()