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 › numpy › numpy_creating_arrays.asp
NumPy Creating Arrays
In this array the innermost dimension (5th dim) has 4 elements, the 4th dim has 1 element that is the vector, the 3rd dim has 1 element that is the matrix with the vector, the 2nd dim has 1 element that is 3D array and 1st dim has 1 element ...
🌐
W3Schools
w3schools.in › python-data-science › matrices-in-python
Matrices in Python - W3Schools
These are 2D (two dimensional) data structure. In live projects and real data simulation, you have to keep the data in a sequential or tabular format. Let suppose you want to store data of three employees of three different departments. So in tabular format, it will look something like: In Python, these tables are termed as two-dimensional arrays...
🌐
W3Schools
w3schools.com › python › numpy › numpy_array_iterating.asp
NumPy Array Iterating
import numpy as np arr = np.array([1, 2, 3]) for x in np.nditer(arr, flags=['buffered'], op_dtypes=['S']): print(x) Try it Yourself » · We can use filtering and followed by iteration. Iterate through every scalar element of the 2D array skipping 1 element:
Top answer
1 of 16
1261

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 16
486

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 › python › numpy › numpy_array_reshape.asp
NumPy Array Reshaping
We can reshape an 8 elements 1D array into 4 elements in 2 rows 2D array but we cannot reshape it into a 3 elements 3 rows 2D array as that would require 3x3 = 9 elements.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-using-2d-arrays-lists-the-right-way
Using 2D arrays/lists in Python - GeeksforGeeks
Python creates only one inner list and one 0 object, not separate copies. This shared reference behavior is known as shallow copying (aliasing). If we assign the 0th index to another integer say 1, then a new integer object is created with the value of 1 and then the 0th index now points to this new int object as shown below · Similarly, when we create a 2d array as "arr = [[0]*cols]*rows" we are essentially extending the above analogy.
Published   December 20, 2025
🌐
TutorialsPoint
tutorialspoint.com › home › python_data_structure › python 2d array
Python 2D Array
February 21, 2009 - Explore the concept of 2D arrays in Python, including how to create and manipulate them effectively.
🌐
W3Schools
w3schools.com › python › python_arrays.asp
Python Arrays
Note: This page shows you how to use LISTS as ARRAYS, however, to work with arrays in Python you will have to import a library, like the NumPy library.
Find elsewhere
🌐
Snakify
snakify.org › two-dimensional lists (arrays)
Two-dimensional lists (arrays) - Learn Python 3 - Snakify
We have already tried to explain that a for-loop variable in Python can iterate not only over a range(), but generally over all the elements of any sequence. Sequences in Python are lists and strings (and some other objects that we haven't met yet). Look how you can print a two-dimensional array, using this handy feature of loop for:
🌐
W3Schools Blog
w3schools.blog › home › data structure 2d array
Data Structure 2D Array - W3schools
February 21, 2021 - Column Major ordering: All the columns of the 2D array, in column-major ordering, are stored into the memory contiguously, i.e., the 1st column of the array is first stored into the memory completely, after which the 2nd row of the array is stored into the memory completely and this process continues till the last column.
🌐
Processing
py.processing.org › tutorials › 2dlists
Two-Dimensional Lists \ Tutorials
Python Mode for Processing extends the Processing Development Environment with the Python programming language.
🌐
Scaler
scaler.com › home › topics › 2d array in python
2D Array in Python | Python Two-Dimensional Array - Scaler Topics
October 10, 2025 - Learn about 2D array in python by Scaler Topics. A two-dimensional array in Python is a nested data structure, meaning it is a set of arrays inside another array.
🌐
Javatpoint
javatpoint.com › python-2d-array
Python 2D array - Javatpoint
Python 2D array with python, tutorial, tkinter, button, overview, entry, checkbutton, canvas, frame, environment set-up, first python program, operators, etc.
🌐
W3Schools
w3schools.com › python › python_dsa_lists.asp
Python Lists and Arrays
Sometimes we want to perform actions that are not built into Python. Then we can create our own algorithms. For example, an algorithm can be used to find the lowest value in a list, like in the example below: Create an algorithm to find the lowest value in a list: my_array = [7, 12, 9, 4, 11, 8] minVal = my_array[0] for i in my_array: if i < minVal: minVal = i print('Lowest value:', minVal) Try it Yourself »
🌐
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...
🌐
CodeSignal
codesignal.com › learn › courses › multidimensional-arrays-and-their-traversal-in-python › lessons › exploring-the-dimensions-a-beginners-guide-to-multidimensional-arrays-in-python
A Beginner's Guide to Multidimensional Arrays in Python
In this example, array is a 2-dimensional array, just like a 3-storey 'apartment building,' where every floor is an inner list. ... All indices in Python arrays are 0-based. Let's say you want to visit an apartment on the second floor (index 1) and bring a package to the first unit (index 0) ...
🌐
YouTube
youtube.com › reallifeed
Python 2D arrays and lists - YouTube
How to use 2D Arrays and Lists. Python Programming Beginners series.In this video:- 2D Arrays- 2D ListsTools:The Python Standard Library - https://docs.pyth...
Published   October 24, 2022
🌐
DataCamp
campus.datacamp.com › courses › intro-to-python-for-data-science › chapter-4-numpy
2D NumPy Arrays | Python
The arrays np_height and np_weight are one-dimensional arrays, but it's perfectly possible to create 2 dimensional, three dimensional, heck even seven dimensional arrays! Let's stick to 2 in this video though. You can create a 2D numpy array from a regular Python list of lists.
🌐
W3Schools
w3schools.com › python › gloss_python_array_methods.asp
Python Array Methods
Python has a set of built-in methods that you can use on lists/arrays.