You're technically trying to index an uninitialized array. You have to first initialize the outer list with lists before adding items; Python calls this "list comprehension".

# Creates a list containing 5 lists, each of 8 items, all set to 0
w, h = 8, 5
Matrix = [[0 for x in range(w)] for y in range(h)] 

#You can now add items to the list:

Matrix[0][0] = 1
Matrix[6][0] = 3 # error! range... 
Matrix[0][6] = 3 # valid

Note that the matrix is "y" address major, in other words, the "y index" comes before the "x index".

print Matrix[0][0] # prints 1
x, y = 0, 6 
print Matrix[x][y] # prints 3; be careful with indexing! 

Although you can name them as you wish, I look at it this way to avoid some confusion that could arise with the indexing, if you use "x" for both the inner and outer lists, and want a non-square Matrix.

Answer from Manny D on Stack Overflow
🌐
W3Schools
w3schools.com › python › python_arrays.asp
Python Arrays
Note: Python does not have built-in support for Arrays, but Python Lists can be used instead.
Spaces
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
Practice
Practice coding problems on W3Schools. Write code, submit, and get instant feedback.
W3Schools app iOS & Android NEW
The W3Schools app turns coding fundamentals into a game. Bite-sized lessons, streaks and XP. Free on iOS and Android.
Python For Loops
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
🌐
W3Schools
w3schools.com › python › numpy › numpy_creating_arrays.asp
NumPy Creating Arrays
NumPy Arrays provides the ndim attribute that returns an integer that tells us how many dimensions the array have.
🌐
W3Schools
w3schools.com › python › python_dsa_lists.asp
Python Lists and Arrays
In Python, lists are the built-in data structure that serves as a dynamic array.
Top answer
1 of 7
1263

You're technically trying to index an uninitialized array. You have to first initialize the outer list with lists before adding items; Python calls this "list comprehension".

# Creates a list containing 5 lists, each of 8 items, all set to 0
w, h = 8, 5
Matrix = [[0 for x in range(w)] for y in range(h)] 

#You can now add items to the list:

Matrix[0][0] = 1
Matrix[6][0] = 3 # error! range... 
Matrix[0][6] = 3 # valid

Note that the matrix is "y" address major, in other words, the "y index" comes before the "x index".

print Matrix[0][0] # prints 1
x, y = 0, 6 
print Matrix[x][y] # prints 3; be careful with indexing! 

Although you can name them as you wish, I look at it this way to avoid some confusion that could arise with the indexing, if you use "x" for both the inner and outer lists, and want a non-square Matrix.

2 of 7
487

If you really want a matrix, you might be better off using numpy. Matrix operations in numpy most often use an array type with two dimensions. There are many ways to create a new array; one of the most useful is the zeros function, which takes a shape parameter and returns an array of the given shape, with the values initialized to zero:

>>> import numpy
>>> numpy.zeros((5, 5))
array([[ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.]])

Here are some other ways to create 2-d arrays and matrices (with output removed for compactness):

numpy.arange(25).reshape((5, 5))         # create a 1-d range and reshape
numpy.array(range(25)).reshape((5, 5))   # pass a Python range and reshape
numpy.array([5] * 25).reshape((5, 5))    # pass a Python list and reshape
numpy.empty((5, 5))                      # allocate, but don't initialize
numpy.ones((5, 5))                       # initialize with ones

numpy provides a matrix type as well, but it is no longer recommended for any use, and may be removed from numpy in the future.

🌐
W3Schools
w3schools.com › c › c_arrays_multi.php
C Multidimensional Arrays (Two-dimensional and more)
Multidimensional arrays are useful ... level of structure: 2D arrays (like int scores[3][4]) are great for storing things like scores, game boards, or spreadsheets...
🌐
Programmingforlovers
programmingforlovers.com › home › chapter 3: discovering a self-replicating automaton with top-down programming › chapter 3 python code alongs › introduction to two-dimensional arrays in python
Introduction to Two-Dimensional Arrays in Python - Programming for Lovers
August 28, 2026 - When we run our code once more, we see that we have added the array of five False values as shown below. Consider the following function that takes as input a two-dimensional boolean list a and sets its top left element to True, without returning the list. Note that the type hint for a is a: list[list[bool]]. def set_first_element_to_true(a: list[list[bool]]) -> None: """ Set the top-left element of a 2D boolean list to True.
🌐
W3Schools
w3schools.com › python › numpy › numpy_array_iterating.asp
NumPy Array Iterating
As we deal with multi-dimensional arrays in numpy, we can do this using basic for loop of python.
Find elsewhere
🌐
Cf
physics-python.astro.cf.ac.uk › Week6
PX1224 -Week6: Two Dimensional Arrays
A 2D array can be "sliced" to return a 1D array containing one row or column of the original. So, for example, the zeroth (Python starts array with index 0) row or column is given by
🌐
W3Schools
w3schoolsua.github.io › python › python_arrays_en.html
Python Arrays. Lessons for beginners. W3Schools in English
Python Arrays. What is an Array? Access the Elements of an Array. The Length of an Array. Looping Array Elements. Adding Array Elements. Removing Array Elements. Array Methods. Python does not have built-in support for Arrays, but Python Lists can be used instead.
🌐
AskPython
askpython.com › python › two-dimensional-array-in-python
Two Dimensional Array in Python - AskPython
December 27, 2019 - Output-Append 2D Array · Array slicing is used to access multiple values within an array. Syntax: <slice_array> = <array>[start:stop] array1 = [[1,2,3],[4,5,6,7]] #python array slice array2 = array1[1:3] #index 1 to 2 print(array2) array2 = ...
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › 2d array in python
Comprehensive Guide to 2D Arrays in Python: Creation, Access, and Use
November 11, 2024 - To print a 2D array as a grid in Python, you can use nested loops to iterate through the rows and columns of the array and format the output as a grid. Here's a simple example of Python print 2d array as grid:
🌐
TutorialsPoint
tutorialspoint.com › python_data_structure › python_2darray.htm
Python - 2-D Array
Two dimensional array is an array within an array. It is an array of arrays. In this type of array the position of an data element is referred by two indices instead of one. So it represents a table with rows an dcolumns of data.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-using-2d-arrays-lists-the-right-way
Using 2D arrays/lists in Python - GeeksforGeeks
The code below, compares two ways of initializing a 2D list in Python. Using list multiplication ([[0]*cols]*rows) creates multiple references to the same inner list, causing aliasing where changes affect all rows. Using a nested list comprehension creates a separate list for each row, avoiding aliasing and correctly forming a 2D array.
Published: December 20, 2025
🌐
GeeksforGeeks
geeksforgeeks.org › python › multi-dimensional-lists-in-python
Multi-dimensional Lists in Python - GeeksforGeeks
October 30, 2025 - In Python, a Multi-dimensional List is a list containing other lists, often used to represent structured data like matrices, tables or 2D arrays.
🌐
Medium
medium.com › @bharathurs42 › creating-a-2d-lists-python-1d3c1ad57235
Creating a 2D-list in Python. This is how easy it is to create a 2D… | by Bharath Urs | Medium
January 26, 2023 - There are many things that are easier in Python, but creating a 2D list is not one of them. Here’s how to do it: ... Just a beautiful single line ! I can perhaps use any iterable item instead of range() ... W3Schools has a great example for list comprehension; they have mixed it with an if condition, to create a 2nd list (newlist):
🌐
Scaler
scaler.com › home › topics › 2d array in python
2D Array in Python | Python Two-Dimensional Array - Scaler Topics
May 25, 2026 - Unlike a one dimensional array, which uses a single index for individual elements, a 2D array uses two indices to locate a value. 2D arrays in python are zero-indexed, which means counting indices start from zero rather than one; thus, zero is the first index in an array in python.
🌐
Processing
py.processing.org › tutorials › 2dlists
Two-Dimensional Lists \ Tutorials
Python Mode for Processing extends the Processing Development Environment with the Python programming language.
🌐
PyTutorial
pytutorial.com › python-2d-arrays-guide-lists-numpy-examples
PyTutorial | Python 2D Arrays Guide: Lists, NumPy, Examples
March 25, 2026 - Each cell in the grid can hold a value. You access a cell using two indices. The first index is for the row. The second index is for the column. Python does not have a built-in 2D array type.