If you restrict yourself to the Python standard library, then a list of lists is the closest construct:

arr = [[1,2],[3,4]]

gives a 2d-like array. The rows can be accessed as arr[i] for i in {0,..,len(arr}, but column access is difficult.

If you are willing to add a library dependency, the NumPy package is what you really want. You can create a fixed-length array from a list of lists using:

import numpy
arr = numpy.array([[1,2],[3,4]])

Column access is the same as for the list-of-lists, but column access is easy: arr[:,i] for i in {0,..,arr.shape[1]} (the number of columns).

In fact NumPy arrays can be n-dimensional.

Empty arrays can be created with

numpy.empty(shape)

where shape is a tuple of size in each dimension; shape=(1,3,2) gives a 3-d array with size 1 in the first dimension, size 3 in the second dimension and 2 in the 3rd dimension.

If you want to store objects in a NumPy array, you can do that as well:

 arr = numpy.empty((1,), dtype=numpy.object)
 arr[0] = 'abc'

For more info on the NumPy project, check out the NumPy homepage.

Answer from Barry Wark on Stack Overflow
🌐
freeCodeCamp
freecodecamp.org › news › multi-dimensional-arrays-in-python
Multi-Dimensional Arrays in Python – Matrices Explained with Examples
December 11, 2025 - In this article, we will focus on NumPy, which is one of the most popular and widely used libraries for working with arrays in Python. NumPy provides a powerful N-dimensional array object that you can use to create and manipulate multi-dimensional arrays efficiently.
🌐
AskPython
askpython.com › python › array › multidimensional-arrays
Multidimensional Arrays in Python: A Complete Guide - AskPython
February 27, 2023 - In this article, the creation and implementation of multidimensional arrays (2D, 3D as well as 4D arrays) have been covered along with examples in Python Programming language. To understand and implement multi-dimensional arrays in Python, the NumPy package is used.
Discussions

python - How to define a two-dimensional array? - Stack Overflow
How to initialize a two-dimensional ... in Python? (32 answers) List of lists changes reflected across sublists unexpectedly (18 answers) Closed 2 years ago. I want to define a two-dimensional array without an initialized length like this: ... One does not define arrays, or any other thing. You can, however, create multidimensional sequences, ... More on stackoverflow.com
🌐 stackoverflow.com
How to initialize a two-dimensional array (list of lists, if not using NumPy) in Python? - Stack Overflow
I really like languages that are explicit about what is a reference and what is a copy (or lazy copy). python just makes it confusing 2024-08-04T03:14:03.807Z+00:00 ... Save this answer. ... Show activity on this post. Usually when you want multidimensional arrays you don't want a list of lists, ... More on stackoverflow.com
🌐 stackoverflow.com
Multidimensional arrays?
If you just use normal lists, you can store whatever you like. data = [ [1, 2, 3], [4], [5, 6, 7, 8, 9, 10], ] And you can refer to them exactly as you say: data[2][3] which will give you 8. More on reddit.com
🌐 r/learnpython
10
1
September 15, 2023
[2023 Day 10] X-axis, Y-axis and order of indexes of 2D-arrays
I use C++, but I'll try to translate that into python, too. grid.at(x,y) stored in a std::vector (a list in python) and an accessor that does x+y*width. since i'm using c++23 i can even use grid[x, y] as accessor. Really sparse grids I store in a std::map (dictionary in python). More on reddit.com
🌐 r/adventofcode
30
2
December 11, 2023
🌐
CodeSignal
codesignal.com › learn › courses › multidimensional-arrays-and-their-traversal-in-python › lessons › exploring-the-dimensions-a-beginners-guide-to-multidimensional-arrays-in-python
Exploring the Dimensions: A Beginner's Guide to Multidimensional Arrays in Python | CodeSignal Learn
Our goal today is to strengthen your foundational knowledge of these 'apartment buildings' and how to handle them effectively in Python. To construct a multidimensional or nested array in Python, we use lists inside lists.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › python tutorial › multidimensional array in python
Multidimensional Array in Python | Creating a Multidimensional List
April 17, 2023 - In Python, Multidimensional Array can be implemented by fitting in a list function inside another list function, which is basically a nesting operation for the list function. Here, a list can have a number of values of any data type that are ...
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
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.

🌐
Medium
soumenatta.medium.com › how-to-work-with-multidimensional-arrays-in-python-a-beginners-guide-bd437495c87
How to Work with Multidimensional Arrays in Python: A Beginner’s Guide | by Dr. Soumen Atta, Ph.D. | Medium
April 8, 2023 - One way to create a multidimensional array in Python is by using a list of lists. Each element in the list represents a row in the array, and each sub-list represents the elements in that row.
Find elsewhere
🌐
University at Buffalo
math.buffalo.edu › ~badzioch › MTH337 › PT › PT-multidimensional_numpy_arrays › PT-multidimensional_numpy_arrays.html
Multidimensional numpy arrays — MTH 337
a = np.reshape(np.arange(30), (5,6)) # create a 5x6 array print(a) [[ 0 1 2 3 4 5] [ 6 7 8 9 10 11] [12 13 14 15 16 17] [18 19 20 21 22 23] [24 25 26 27 28 29]] b = a[1:4, 0:2] #select elements in rows 1-3 and columns 0-1 print(b) ... Note. a[i] is the same as a[i,:] i.e. it selects the i-th row of the array: ... Similarly as for 1-dimensional arrays slicing produces a view of the original array, and changing a slice changes the original array:
🌐
GeeksforGeeks
geeksforgeeks.org › python › multi-dimensional-lists-in-python
Multi-dimensional Lists in Python - GeeksforGeeks
October 30, 2025 - It’s useful for storing and accessing data in rows and columns, commonly applied in data analysis, mathematics and image processing. ... A multidimensional list is created by nesting lists within a single list.
🌐
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
🌐
Snakify
snakify.org › two-dimensional lists (arrays)
Two-dimensional lists (arrays) - Learn Python 3 - Snakify
[say more on this!] Such tables are called matrices or two-dimensional arrays. In Python any table can be represented as a list of lists (a list, where each element is in turn a list). For example, here's the program that creates a numerical table with two rows and three columns, and then makes some manipulations with it:
🌐
Python Like You Mean It
pythonlikeyoumeanit.com › Module3_IntroducingNumpy › AccessingDataAlongMultipleDimensions.html
Accessing Data Along Multiple Dimensions in an Array — Python Like You Mean It
# an array with 5 numbers are cannot be reshaped # into a (3, 2) array >>> np.array([0, 1, 2, 3, 4]).reshape(3, 2) ValueError: total size of new array must be unchanged ... # reshaping a multidimensional array >>> x = np.array([[ 0, 1, 2, 3], ... [ 4, 5, 6, 7], ...
🌐
Reddit
reddit.com › r/learnpython › multidimensional arrays?
r/learnpython on Reddit: Multidimensional arrays?
September 15, 2023 -

Hello!

I have 5 sets, each set contains a number of elements of variable length (eg. set 1 has 100 elements, set 2 has 150 and so on)

Is it possible to store these sets in a single structure?

For example, if I want to print the 35th elements of the 3rd set, I could call it simply by saying something like MyContainer[setN][elementN]

I was trying to use a 2D array but I can't initialize its shape, since each set has a variable number of elements.

Can anybody point me to the right direction / best practice?

Thank you

🌐
Sentry
sentry.io › sentry answers › python › define a two-dimensional array in python
Python Define a Two-Dimensional Array or Matrix | Sentry
September 23, 2026 - 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
🌐
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 - Once installed, you can import the Numpy module and use its functions and modules to work with multidimensional arrays. To create a 2D array loaded with zeros, you can simply use the numpy.zeros() method.
🌐
iO Flood
ioflood.com › blog › python-2d-array
Python 2D Array with Lists | Guide (With Examples)
February 10, 2024 - In this example, we’ve created a 2D array with three rows and three columns. Each inner list represents a row in the 2D array. To access elements in a 2D array, you need two indices – one for the row and one for the column. The first index refers to the row number and the second index refers to the column number. Let’s access the element in the second row, third column (remember, Python indexing starts at 0):
🌐
GeeksforGeeks
geeksforgeeks.org › python-using-2d-arrays-lists-the-right-way
Python | Using 2D arrays/lists the right way - GeeksforGeeks
Manually initializing and populating a list without using any advanced features or constructs in Python is known as creating a 1D list using "Naive Methods". ... Here we are multiplying the number of rows by the empty list and hence the entire list is created with every element zero. ... Using 2D arrays/lists the right way involves understanding the structure, accessing elements, and efficiently manipulating data in a two-dimensional grid.
Published: June 20, 2024
🌐
GeeksforGeeks
geeksforgeeks.org › multi-dimensional-lists-in-python
Multi-dimensional lists in Python - GeeksforGeeks
December 9, 2018 - In this article, we will cover the Indexing of Multi-dimensional arrays in Python using NumPy. NumPy is a general-purpose array-processing package. It provides a high-performance multidimensional array object and tools for working with these arrays.