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
Top answer
1 of 16
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 16
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.

🌐
GeeksforGeeks
geeksforgeeks.org › python › python-using-2d-arrays-lists-the-right-way
Using 2D arrays/lists in Python - GeeksforGeeks
Python provides flexible data structures such as lists, which can be used to represent 1D and 2D arrays. A 2D list in Python is essentially a list of lists, commonly used to store data in a table-like format with rows and columns.
Published: December 20, 2025
Discussions

Need help with a two-dimensional array
Hello there! I’m an experienced coder who’s just getting into Python for the first time. I know several versions of BASIC, Pascal, C, C++, C#, PHP, MySQL… and so on. Here’s what I have: 1 2 3 4 5 6 7 8 A X X X X X X X X B X X X X X T X X C X X X X X X X X D X X X X X X X X E X X X X ... More on discuss.python.org
🌐 discuss.python.org
4
0
June 3, 2022
2D arrays in Python - Stack Overflow
In Python one would usually use lists for this purpose. Lists can be nested arbitrarily, thus allowing the creation of a 2D array. Not every sublist needs to be the same size, so that solves your other problem. More on stackoverflow.com
🌐 stackoverflow.com
2 D Arrays in Python

Pay attention to what for y in x does. It doesn't give you array indices, instead it gives you actual elements.

More on reddit.com
🌐 r/learnprogramming
4
0
July 19, 2018
Struggling with 2d arrays.
I was able to understand 2D arrays much better after realizing they’re basically just a grid. Imagine a 4x4 grid. That grid will range from my_array[0,0] in the top left box, all the way up to my_array[3,3] in the bottom right. In my example, my_array[0, ] are the columns and my_array[ ,0] are the rows. More on reddit.com
🌐 r/learnpython
9
1
November 25, 2024
🌐
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.
🌐
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.
🌐
Snakify
snakify.org › two-dimensional lists (arrays)
Two-dimensional lists (arrays) - Learn Python 3 - Snakify
To process 2-dimensional array, you typically use nested loops. The first loop iterates through the row number, the second loop runs through the elements inside of a row.
🌐
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.
🌐
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
To construct a multidimensional or nested array in Python, we use lists inside lists. Here's an example of a 2-dimensional array: # Creating a 2D array array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] print(array) In this example, array is a 2-dimensional array, just like a 3-storey 'apartment building,' ...
Find elsewhere
🌐
Guru99
guru99.com › home › python › python 2d arrays: two-dimensional list examples
Python 2D Arrays: Two-Dimensional List Examples
July 10, 2026 - Python 2D Array is an array placed inside another array, forming rows and columns like a table.
🌐
Javatpoint
javatpoint.com › python-2d-array
Python 2D array - Javatpoint
January 10, 2021 - Python 2D array with python, tutorial, tkinter, button, overview, entry, checkbutton, canvas, frame, environment set-up, first python program, operators, etc.
🌐
Python Guides
pythonguides.com › create-a-2d-array-in-python
2D Array in Python: Nested Lists and NumPy Examples
May 9, 2025 - A 2D array in Python is a table of values arranged in rows and columns. Python has no built-in 2D array type, so you use either a nested list (a list of rows, like [[1, 2], [3, 4]]) or a NumPy 2D array created with np.array(), np.zeros() and ...
🌐
EDUCBA
educba.com › home › software development › software development tutorials › python tutorial › 2d arrays in python
2D Arrays In Python | Different operations in 2D arrays with Sample Code
June 29, 2023 - It can effectively perform the simplest operations like addition and subtraction to the toughest tasks like multiplication and inverse operations. Based on the requirement, they are termed two-dimensional arrays in the Python programming language in the context of data analysis.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Python.org
discuss.python.org › python help
Need help with a two-dimensional array - Python Help - Discussions on Python.org
June 3, 2022 - Hello there! I’m an experienced coder who’s just getting into Python for the first time. I know several versions of BASIC, Pascal, C, C++, C#, PHP, MySQL… and so on. Here’s what I have: 1 2 3 4 5 6 7 8 A X X X X X X X X B X X X X X T X X C X X X X X X X X D X X X X X X X X E X X X X X X X X F X X X X X X X X G X X X X X X X X H X X X X X X X X This is a simple two-dimensional array.
🌐
Edureka
edureka.co › blog › 2d-arrays-in-python
2D Arrays In Python | Implementing Arrays In Python | Edureka
November 27, 2024 - So we all know arrays are not present as a separate object in python but we can use list object to define and use it as an array. For example, consider an array of ten numbers: A = {1,2,3,4,5} Syntax used to declare an array: ... Moving with this article on 2D arrays in Python.
🌐
AskPython
askpython.com › python › two-dimensional-array-in-python
Two Dimensional Array in Python - AskPython
August 6, 2022 - 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 = array1[:1] #index 0 to 1 print(array2) Output: Output-Slicing 2D Array ·
🌐
GeeksforGeeks
geeksforgeeks.org › python-using-2d-arrays-lists-the-right-way
Python | Using 2D arrays/lists the right way - GeeksforGeeks
A 2D array is essentially a list of lists, which represents a table-like structure with rows and columns. In Python, Initializing a collection of elements in a linear sequence requires creating a 1D array, which is a fundamental process.
Published: June 20, 2024
🌐
Drbeane
drbeane.github.io › python_dsci › pages › array_2d.html
2-Dimensional Arrays — Python for Data Science
The first number indexes a row in the array and the second number indexes a column. ... We can also use slicing with 2D arrays.
🌐
Sentry
sentry.io › sentry answers › python › define a two-dimensional array in python
Define a two-dimensional array in Python
June 15, 2023 - Create a 2D array in Python using numpy.zeros for matrix operations, or a nested list comprehension if you want to avoid the numpy dependency
🌐
Google
sites.google.com › rgc.aberdeen.sch.uk › rgcahcomputingrevision › software-design › data-types-and-structures › 2d-arrays
Advanced Higher Computing Revision - 2D Arrays
We could use the len function to find the number of rows and columns. As Python stores a 2D array as a list of lists len(my2dArray) returns the amount of rows and len(my2dArray[i]) returns the number of columns (in that row).
🌐
Finxter
blog.finxter.com › home › learn python blog › how to create a two dimensional array in python?
How To Create a Two Dimensional Array in Python? - Be on the Right Side of Change
June 11, 2022 - Finally, we print the array with the help of a for loop by simply displaying the keys of the dictionary which are storing the lists, i.e., the individual rows of the 2D array. The most convenient way of working with arrays in Python is to use Python’s Numpy library.