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
🌐
NumPy
numpy.org › doc › stable › 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!
🌐
NumPy
numpy.org › devdocs › user › absolute_beginners.html
NumPy: the absolute basics for beginners — NumPy v2.5.dev0 Manual
The shape must be “rectangular”, ... row of a two-dimensional array must have the same number of columns. When these conditions are met, NumPy exploits these characteristics to make the array faster, more memory efficient, and more convenient to use than less restrictive data structures. For the remainder of this document, we will use the word “array” to refer to an instance of ndarray. One way to initialize an array is using a Python sequence, ...
🌐
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.
🌐
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 ...
🌐
w3resource
w3resource.com › python-exercises › numpy › python-numpy-exercise-37.php
Python NumPy: Create a 2-dimensional array of size 2 x 3 - w3resource
Write a NumPy program to create a 2-dimensional array of size 2 x 3 (composed of 4-byte integer elements), also print the shape, type and data type of the array. ... # Importing the NumPy library with an alias 'np' import numpy as np # Creating ...
🌐
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.
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.

Find elsewhere
🌐
Sentry
sentry.io › sentry answers › python › define a two-dimensional array in python
Define a two-dimensional array in Python | Sentry
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....
🌐
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 …
🌐
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:
🌐
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 Examples
pythonexamples.org › python-numpy-create-2d-array
Create 2D Array in NumPy
In this NumPy Tutorial, we learned how to create a 2D numpy array in Python using different NumPy functions.
🌐
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…
🌐
W3Schools
w3schools.com › python › numpy › numpy_creating_arrays.asp
NumPy Creating Arrays
NumPy has a whole sub module dedicated towards matrix operations called numpy.mat · Create a 2-D array containing two arrays with the values 1,2,3 and 4,5,6:
🌐
AskPython
askpython.com › home › multidimensional arrays in python: a complete guide
Multidimensional Arrays in Python: A Complete Guide - AskPython
February 27, 2023 - #To install the Numpy package pip install numpy #To import the Numpy package import numpy as np · An array of arrays is a simple definition that applies to the two-dimensional array.
🌐
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.
🌐
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.
🌐
freeCodeCamp
freecodecamp.org › news › multi-dimensional-arrays-in-python
Multi-Dimensional Arrays in Python – Matrices Explained with Examples
December 11, 2025 - We'll now look at some examples of how to create and work with multi-dimensional arrays in Python using NumPy.
🌐
University at Buffalo
math.buffalo.edu › ~badzioch › MTH337 › PT › PT-multidimensional_numpy_arrays › PT-multidimensional_numpy_arrays.html
Multidimensional numpy arrays — MTH 337
Notice that array multiplication multiplies corresponding elements of arrays. In order to perform matrix multiplication of 2-dimensional arrays we can use the numpy dot() function: