What's happening here is that a reference to the same list (the one obtained from [0] * 3) is being placed 3 times in the new list, rather than 3 different lists being placed in it. A way to do what you want is:

a = [[[0] * 3] for _ in range(3)]
Answer from Addison Schmidt on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-matrix
Python - Matrix - GeeksforGeeks
July 23, 2025 - The size of a matrix is defined by the number of rows (m) and columns (n). If a matrix has 3 rows and 4 columns, it's called a 3x4 matrix. ... In this tutorial, weโ€™ll explore different ways to create and work with matrices in Python, including using the NumPy library for matrix operations.
Top answer
1 of 4
4

What's happening here is that a reference to the same list (the one obtained from [0] * 3) is being placed 3 times in the new list, rather than 3 different lists being placed in it. A way to do what you want is:

a = [[[0] * 3] for _ in range(3)]
2 of 4
2

With all below code snippets you can create a 3 * 3 matrix with python:

First method: Appending columns inside of loops for each row

rows = 3
cols = 3

lst = []
matrix = []
for i in range(rows):
    for j in range(cols):
        lst.append(0)
    matrix.append(lst)
    lst.clear()

print(matrix)
# the output will be: [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

matrix[1][1] = 1
print(matrix)
# the output will be: [[0, 0, 0], [0, 1, 0], [0, 0, 0]]

Second method: 1 list comprehension outside and 1 concatenation operation inside

rows = 3
cols = 3
 
matrix = [[0] * cols for _ in range(rows)]

print(matrix)
# the output will be: [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

matrix[1][1] = 1
print(matrix)
# the output will be: [[0, 0, 0], [0, 1, 0], [0, 0, 0]]

Third method: 1 list comprehension inside and 1 concatenation operation outside

rows = 3
cols = 3
 
matrix = [[0 for _ in range(cols)]] * rows

print(matrix)
# the output will be: [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

matrix[1][1] = 1
print(matrix)
# the output will be: [[0, 0, 0], [0, 1, 0], [0, 0, 0]]

Fourth method: Using numpy package

import numpy as np
 
rows = 3
cols = 3
size = rows*cols
 
matrix = np.array([0]*size).reshape(rows,cols)

print(matrix)
# the output will be: [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

matrix[1][1] = 1
print(matrix)
# the output will be: [[0, 0, 0], [0, 1, 0], [0, 0, 0]]
๐ŸŒ
YouTube
youtube.com โ€บ watch
Python Tutorial: Matrix - YouTube
Come join 1,000s of Developers and other tech professionals learning the most in-demand tech skills (including Python!) at Zero To Mastery: https://zerotomas...
Published: August 24, 2024
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ matrix
Python Matrix and Introduction to NumPy
You can treat lists of a list (nested list) as matrix in Python. However, there is a better way of working Python matrices using NumPy package. NumPy is a package for scientific computing which has support for a powerful N-dimensional array object.
๐ŸŒ
YouTube
youtube.com โ€บ watch
Matrices | Programming Fundamentals with Python - YouTube
All JomaClass videos from 2020 are now free to watch. If you enjoy please consider donating here: https://support.joma.io/ This video was uploaded in 2020 in...
Published: October 6, 2025
Find elsewhere
๐ŸŒ
Medium
medium.com โ€บ @zackbunch โ€บ matrices-with-numpy-and-scipy-a260a65551c6
Python Matrices with NumPy and SciPy | Medium
January 11, 2022 - NumPy also provides array operations that are faster than comparable Python operations on lists, along with optimized functions for mathematics that require NumPy arrays as their inputs. For more information about NumPy arrays, see the official NumPy documentation. NumPyโ€™s array method creates arrays like the two-dimensional one shown below. These arrays will be used throughout this unit to represent matrices. # Using numpy's array() method to define a two dimensional matrix import numpy as np A = np.array([[1,2,3], # Define a matrix, [4,5,6], # where each row in the matrix is defined using a Python list [7,8,9]]) A # Display the resulting matrix
๐ŸŒ
Medium
medium.com โ€บ @dr.aleksei.krasnov โ€บ all-you-need-to-know-about-the-basics-of-matrices-in-python-part-1-ea9dc958d56e
All you need to know about the basics of Matrices in Python: Part 1 | by Dr. Aleksei Krasnov | Medium
November 18, 2024 - Matrix properties: addition and subtraction, scalar multiplication, matrix multiplication (with rules and non-commutative property), and multiplication by the identity matrix. Python implementation by using NumPy for efficient matrix operations.
๐ŸŒ
YouTube
youtube.com โ€บ telusko
#31 Python Tutorial for Beginners | Working with Matrix in Python - YouTube
Matrix Multiplication Theory : https://goo.gl/omPVAS Watch till 7:12 mins Python Tutorial to learn Python programming with examples Complete Python Tutorial ...
Published: July 23, 2018
Views: 338K
๐ŸŒ
Python Course
python-course.eu โ€บ numerical-programming โ€บ matrix-arithmetics-under-numpy-and-python.php
10. Matrix Arithmetics under NumPy and Python | Numerical Programming
March 24, 2022 - In the previous chapter of our introduction in NumPy we have demonstrated how to create and change Arrays. In this chapter we want to show, how we can perform in Python with the module NumPy all the basic Matrix Arithmetics like
๐ŸŒ
CodeGym
codegym.cc โ€บ java blog โ€บ learning python โ€บ matrix in python
Matrix in Python
November 6, 2024 - The simplest way to create a matrix in Python is by using nested lists.
๐ŸŒ
W3Schools
w3schools.in โ€บ python-data-science โ€บ matrices-in-python
Matrices in Python
Python allows developers to implement matrices using the nested list. Lists can be created if you place all items or elements starting with '[' and ending with ']' (square brackets) and separate each element by a comma. val = [['Dave',101,90,95], ['Alex',102,85,100], ['Ray',103,90,95]] It is ...
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ python matrix
Python Matrix - Scaler Topics
September 25, 2023 - Note: While Python doesn't have a built-in matrix type, it provides the flexibility to treat a list of lists as a matrix in Python. With this approach, Python enables programmers to work with matrices efficiently.
๐ŸŒ
Medium
medium.com โ€บ @cssjhnnamae โ€บ implementing-matrices-in-data-structures-using-python-d71613a3bfa5
Implementing Matrices in Data Structures using Python | by Princess Rodiel | Medium
August 22, 2024 - In Python, a matrix is essentially a two-dimensional Numpy array, which holds data in rows and columns. The rows in a Python matrix are the horizontal sequences of items, and the columns are the vertical sequences of items.