This works:

a = [[1, 2, 3], [4, 5, 6]]
nd_a = np.array(a)

So this should work too:

nd_a = np.array([[x for x in y] for y in a])
Answer from Marijn van Vliet on Stack Overflow
🌐
DataCamp
campus.datacamp.com › courses › intro-to-python-for-data-science › chapter-4-numpy
2D NumPy Arrays | Python
Let's stick to 2 in this video though. You can create a 2D numpy array from a regular Python list of lists. Let's try to create one numpy array for all height and weight data of your family, like this. If you print out np_2d now, you'll see that it is a rectangular data structure: Each sublist ...
🌐
Drbeane
drbeane.github.io › python_dsci › pages › array_2d.html
2-Dimensional Arrays — Python for Data Science
In this lesson, we will work exclusively with 2D arrays, which consist of several values arranged into ordered rows and columns. You can create a two dimensional array by applying np.array() to a list of lists, as long as the sublists are of the same size, and contain elements of a single data type.
🌐
NumPy
numpy.org › doc › stable › reference › arrays.ndarray.html
The N-dimensional array (ndarray) — NumPy v2.5 Manual
That is, an ndarray can be a “view” to another ndarray, and the data it is referring to is taken care of by the “base” ndarray. ndarrays can also be views to memory owned by Python strings or objects implementing the memoryview or array interfaces. ... Try it in your browser!
🌐
Medium
medium.com › @bouimouass.o › what-does-a-numpy-2d-array-look-like-in-python-38752ccde895
What does a numpy 2D array look like in python? | by Omar | Medium
July 23, 2023 - What does a numpy 2D array look like in python? A NumPy 2D array is a rectangular array of data. It is a two-dimensional array, which means it has rows and columns. The rows are represented by the …
🌐
NumPy
numpy.org › devdocs › user › absolute_beginners.html
NumPy: the absolute basics for beginners — NumPy v2.6.dev0 Manual
You might hear of a 0-D (zero-dimensional) array referred to as a “scalar”, a 1-D (one-dimensional) array as a “vector”, a 2-D (two-dimensional) array as a “matrix”, or an N-D (N-dimensional, where “N” is typically an integer greater than 2) array as a “tensor”. For clarity, it is best to avoid the mathematical terms when referring to an array because the mathematical objects with these names behave differently than arrays (e.g. “matrix” multiplication is fundamentally different from “array” multiplication), and there are other objects in the scientific Python ecosystem that have these names (e.g.
🌐
OpenGenus
iq.opengenus.org › 2d-array-in-numpy
2D Arrays in NumPy (Python)
October 28, 2022 - before = np.array([[1,2,3,4],[5,6,7,8]]) #it's dimensions are 2x4 after = before.reshape(4,2) #it's dimensions are 4x2 print(after) ... As we want first two rows and columns we will start indexing from 0 and it will end at 2.
🌐
Python Guides
pythonguides.com › python-numpy-2d-array
Create A 2D NumPy Array In Python (5 Simple Methods)
May 16, 2025 - In this article, I’ll show you five easy methods to create 2D NumPy arrays (also known as matrices) based on my decade of experience working with Python.
Find elsewhere
🌐
AskPython
askpython.com › python › array › multidimensional-arrays
Multidimensional Arrays in Python: A Complete Guide - AskPython
February 27, 2023 - The 2D array can be visualized as a table (a square or rectangle) with rows and columns of elements. The image below depicts the structure of the two-dimensional array. ... Let’s start with implementing a 2 dimensional array using the numpy ...
🌐
MLJAR
mljar.com › answers › define-two-dimensional-array-python
Define two-dimensional array in Python
w = 6 # width, number of columns h = 4 # height, number of rows array = [[0 for x in range(w)] for y in range(h)] ... REMEMBER Be careful with idexing, you can't go out of range! ... This way is much easier. You can use zeros function from numpy to create 2D array with all values set to zero:
🌐
DataCamp
campus.datacamp.com › courses › introduction-to-python-for-finance › arrays-in-python
2D arrays and functions | Python
Often financial or quantitative data comes in the form of a table, with rows and columns. It's natural to represent this type of data in a 2D array. To create a 2D array in NumPy, you can use the same array() function you used earlier. But instead of providing a single list as the input, you ...
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.

🌐
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 - Explanation: Convert the list into a numpy array object with the help of the np.array() method and then use the reshape() method to convert this array to a 2D array. Can We Reshape Into any Shape?
🌐
Data Science Dojo
discuss.datasciencedojo.com › python
How to create a two-dimensional array using NumPy? - Python - Data Science Dojo Discussions
May 15, 2023 - I have comprehended multiple methods that assist me in defining a one-dimensional array using NumPy. Now, I aspire to master methods that help in developing a two-dimensional array. For this purpose, I have found one met…
🌐
Sentry
sentry.io › sentry answers › python › define a two-dimensional array in python
Define a two-dimensional array in Python | Sentry
June 15, 2023 - The best way to create two-dimensional ... functionality for manipulating multidimensional arrays. We can create a 2D array by passing a tuple to the zeros function....
🌐
Kaggle
kaggle.com › code › ehsanbouji › two-dimensional-numpy-array
Two Dimensional Numpy Array
April 15, 2025 - 2D Numpy in PythonObjectivesTable of ContentsCreate a 2D Numpy ArrayAccessing different elements of a Numpy ArrayBasic OperationsAuthorOther contributorsChange Log
🌐
NumPy
numpy.org › doc › 2.4 › reference › arrays.ndarray.html
The N-dimensional array (ndarray) — NumPy v2.4 Manual
That is, an ndarray can be a “view” to another ndarray, and the data it is referring to is taken care of by the “base” ndarray. ndarrays can also be views to memory owned by Python strings or objects implementing the memoryview or array interfaces. ... Try it in your browser!
🌐
Python Like You Mean It
pythonlikeyoumeanit.com › Module3_IntroducingNumpy › AccessingDataAlongMultipleDimensions.html
Accessing Data Along Multiple Dimensions in an Array — Python Like You Mean It
Thus you will see “axes” (pronounced “aks-ēz”) used in place of “dimensions”; however, they mean the same thing. NumPy specifies the row-axis (students) of a 2D array as “axis-0” and the column-axis (exams) as axis-1. You must now provide two indices, one for each axis (dimension), to uniquely specify an element in this 2D array; the first number specifies an index along axis-0, the second specifies an index along axis-1.
🌐
Sanshaacademy
sanshaacademy.com › python › ds › two_d_array.php
Two Dimensional (2D) Array in Python
# Creating a 2D array (list of lists) matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] # Accessing elements print(matrix[0][1]) # Output: 2 (row 0, column 1) # Modifying an element matrix[1][2] = 10 · for row in matrix: for element in row: print(element, end=" ") print() # Adding a new row matrix.append([10, 11, 12]) # Adding a new column to each row for row in matrix: row.append(0) import numpy as np # Creating a 2D array with NumPy arr = np.array([[1, 2], [3, 4]]) # Access and modify print(arr[1, 0]) # Output: 3 arr[0, 1] = 10
🌐
freeCodeCamp
freecodecamp.org › news › multi-dimensional-arrays-in-python
Multi-Dimensional Arrays in Python – Matrices Explained with Examples
December 11, 2025 - In Python, you can create multi-dimensional arrays using various libraries, such as NumPy, Pandas, and TensorFlow.