# 🔢 Understanding Python List-Based Matrix Multiplication — A Deep Dive

Yeeun·2025년 4월 11일

Python

목록 보기
3/31

🔢 Understanding Python List-Based Matrix Multiplication — A Deep Dive

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:

  1. Why does range(1) create one row?
  2. What does b[0] mean in a matrix multiplication list comprehension?

Let’s go!


📌 Part 1: Why Does 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)

✅ Output:

[[1, 1, 1, 1, 1]]

❓Question:

Wait... doesn't range(1) only give 0? Shouldn't this produce nothing?

✅ Explanation:

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:

  1. range(1)[0] → loop runs once
  2. Inner list comprehension: [1 for _ in range(5)][1, 1, 1, 1, 1]

So the result is:

[[1, 1, 1, 1, 1]]  # A 1x5 matrix

📌 Part 2: Understanding b[0] in Manual Matrix Multiplication

Now 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)

✅ Output:

[[10]]

❓Why 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.


🔄 What Is This Actually Doing?

This line:

sum(a * b[0] for a, b in zip(row, matrix2))

Is performing the dot product between:

  • A row in matrix1
  • A column in matrix2

Example:

[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.


💡 Bonus: NumPy Makes This Easier

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.


✅ Summary

ConceptWhy 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 multiplicationHelps understand the basics of matrix math

✍️ Final Thoughts

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! 😊

0개의 댓글