When you're just starting to work with matrices in Python—especially without using libraries like NumPy—things can quickly get confusing. In this post, I’ll walk you through two key concepts that confused me at first, and we’ll break them down step by step:
range(1) create one row?b[0] mean in a matrix multiplication list comprehension?Let’s go!
range(1) Create One Row?Here’s a small snippet you might have come across:
matrix1 = [[1 for _ in range(5)] for _ in range(1)]
print(matrix1)
[[1, 1, 1, 1, 1]]
Wait... doesn't range(1) only give 0? Shouldn't this produce nothing?
Actually, range(1) means:
[0]
So the outer list comprehension runs once, because range(1) has exactly one element (0).
Here’s how it works step by step:
range(1) → [0] → loop runs once[1 for _ in range(5)] → [1, 1, 1, 1, 1]So the result is:
[[1, 1, 1, 1, 1]] # A 1x5 matrix
b[0] in Manual Matrix MultiplicationNow let’s move on to a slightly more advanced but important concept — doing matrix multiplication manually using Python lists.
Here’s a simplified version:
matrix1 = [[1 for _ in range(5)]]
matrix2 = [[i] for i in range(5)]
# matrix1: 1 x 5
# matrix2: 5 x 1
# Expected result: 1 x 1
f = [[sum(a * b[0] for a, b in zip(row, matrix2))] for row in matrix1]
print(f)
[[10]]
b[0]?Let's break this down. First:
matrix2 = [[0], [1], [2], [3], [4]]
Each element of matrix2 is a list with a single number — not just a number.
So when you do:
for a, b in zip(row, matrix2):
You’re pairing:
a = 1 # from matrix1
b = [0] # from matrix2
Therefore, b is a list like [0], [1], etc. To get the actual number, you need b[0].
Without it, a * b would raise an error (int * list is not allowed). But a * b[0] is valid and gives you the number you're looking for in a matrix product.
This line:
sum(a * b[0] for a, b in zip(row, matrix2))
Is performing the dot product between:
matrix1matrix2Example:
[1, 1, 1, 1, 1] (1 x 5)
⨉
[ [0],
[1],
[2],
[3],
[4] ] (5 x 1)
=
[ [1×0 + 1×1 + 1×2 + 1×3 + 1×4] ]
=
[ [10] ]
So it’s simulating matrix multiplication using pure Python lists.
If you want to skip all of that manual logic and just do the math:
import numpy as np
matrix1 = np.array([[1, 1, 1, 1, 1]])
matrix2 = np.array([[0], [1], [2], [3], [4]])
result = np.matmul(matrix1, matrix2)
print(result) # [[10]]
NumPy handles the dimensions and multiplication automatically, so you don’t have to worry about indexing into lists.
| Concept | Why It Matters |
|---|---|
range(1) | Gives a list with one element → 1 loop |
b[0] | Extracts the number from a [num] list |
zip() | Helps pair up row and column values |
| List-based multiplication | Helps understand the basics of matrix math |
Understanding matrix multiplication at a low level is super useful, especially before diving deep into libraries like NumPy or frameworks like PyTorch and TensorFlow. Once you grasp this, you'll better understand how dot products, shape alignment, and weight matrices work under the hood.
Let me know if you’d like a visual diagram version of this explanation, or if you'd prefer to continue with NumPy from here! 😊