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
๐ŸŒ
Python Guides
pythonguides.com โ€บ python-numpy-2d-array
Create A 2D NumPy Array In Python (5 Simple Methods)
May 9, 2025 - Cells, rows, columns and slices of a 2D sales array. import numpy as np matrix = [[1, 2], [3, 4]] matrix.append([5, 6]) # add a row to a nested list for row in matrix: row.append(0) # add a column print(matrix) arr = np.array([[1, 2], [3, 4]]) ...
๐ŸŒ
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 - import numpy matrix = numpy.zeros((3,3)) # array([[0., 0., 0.], # [0., 0., 0.], # [0., 0., 0.]]) Note that numpy.matrix is deprecated and should not be used for this operation. To create a 2D array without using numpy, we can initialize a list ...
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ numpy โ€บ numpy_creating_arrays.asp
NumPy Creating Arrays
Like in above code it shows that arr is numpy.ndarray type. To create an ndarray, we can pass a list, tuple or any array-like object into the array() method, and it will be converted into an ndarray: ... A dimension in arrays is one level of array depth (nested arrays). nested array: are arrays that have arrays as their elements. 0-D arrays, or Scalars, are the elements in an array. Each value in an array is a 0-D array.
๐ŸŒ
Drbeane
drbeane.github.io โ€บ python_dsci โ€บ pages โ€บ array_2d.html
2-Dimensional Arrays โ€” Python for Data Science
Notice that the array that is printed above is not displayed in the form of a column. In fact, when slicing a single row or a column out of a 2D array, the result is returned as a simple 1D array. Every Numpy array comes equipped with a shape attribute that we can use to determine the shape of the array.
๐ŸŒ
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 - The arange() function creates a ... into a 2D array. For example, the following code creates a 2D NumPy array with 3 rows and 4 columns using the arange() and reshape() functions:...
๐ŸŒ
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โ€ ... implementing the memoryview or array interfaces. ... Try it in your browser! A 2-dimensional array of size 2 x 3, composed of 4-byte integer elements:...
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ numpy โ€บ python-numpy-exercise-37.php
Python NumPy: Create a 2-dimensional array of size 2 x 3 - w3resource
August 29, 2025 - x = np.array([[2, 4, 6], [6, 8, 10]], np.int32): The current line creates a two-dimensional NumPy array x with two rows and three columns using the provided list of lists.
Find elsewhere
๐ŸŒ
OpenGenus
iq.opengenus.org โ€บ 2d-array-in-numpy
2D Arrays in NumPy (Python)
October 28, 2022 - Random Array random.rand(r,c) - this function will generate an array with all random elements.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ numpy โ€บ ndarray-creation
NumPy N-D Array Creation (With Examples)
May 22, 2023 - The np.zeros() function allows us to create N-D arrays filled with all zeros. For example, ... # create 2D array with 2 rows and 3 columns filled with zeros array1 = np.zeros((2, 3)) print("2-D Array: ") print(array1)
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ multi-dimensional-arrays-in-python
Multi-Dimensional Arrays in Python โ€“ Matrices Explained with Examples
December 11, 2025 - In this example, we first import the NumPy library using the import statement. Then, we create a 2-dimensional array using the np.array() function and pass in a list of lists as an argument.
๐ŸŒ
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.
๐ŸŒ
NumPy
numpy.org โ€บ devdocs โ€บ user โ€บ quickstart.html
NumPy quickstart โ€” NumPy v2.6.dev0 Manual
How do we construct a 2D array from a list of equally-sized row vectors? In MATLAB this is quite easy: if x and y are two vectors of the same length you only need do m=[x;y]. In NumPy this works via the functions column_stack, dstack, hstack and vstack, depending on the dimension in which the stacking is to be done. For example:
๐ŸŒ
MLJAR
mljar.com โ€บ answers โ€บ define-two-dimensional-array-python
Define two-dimensional array in Python
array[2][2] = 5 # valid array[7][2] = 3 # error ยท This way is much easier. You can use zeros function from numpy to create 2D array with all values set to zero: import numpy array = numpy.zeros((4,4)) REMEMBER Even though the second version is easier, the first one is better for this operation unless we need any matrix operations.
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.

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ indexing-multi-dimensional-arrays-in-python-using-numpy
Indexing Multi-dimensional arrays in Python using NumPy - GeeksforGeeks
November 4, 2025 - Python ยท import numpy as np arr ... arr[1:4] -> elements at indices 1, 2, 3 -> [22, 24, 26] We use reshape() with arange() to convert a 1D array into a 2D array....
๐ŸŒ
Medium
medium.com โ€บ @monjurmbm404 โ€บ numpy-series-04-numpy-2d-arrays-matrix-basics-rows-columns-and-subarrays-27503a64b887
NumPy 2D Arrays: Matrix Basics, Rows, Columns and Subarrays | by Engr. Md Monjur Bakth Mazumder | Sep, 2026 | Medium
1 week ago - In the next article, we will move from 2D arrays to 3D NumPy arrays. ... This will prepare us for working with more complex numerical structures used in Machine Learning, Deep Learning, image processing, and scientific computing. This article is part of my NumPy Beginner โ†’ Advanced Data Science Series, where I am building a practical understanding of NumPy step by step. ... The goal is to build a strong foundation for Data Analysis, Data Visualization, Machine Learning, and real-world Python projects.
๐ŸŒ
NumPy
numpy.org โ€บ devdocs โ€บ user โ€บ absolute_beginners.html
NumPy: the absolute basics for beginners โ€” NumPy v2.6.dev0 Manual
This means that a 1D array will become a 2D array, a 2D array will become a 3D array, and so on. ... You can explicitly convert a 1D array to either a row vector or a column vector using np.newaxis. For example, you can convert a 1D array to a row vector by inserting an axis along the first ...
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.2 โ€บ reference โ€บ arrays.ndarray.html
The N-dimensional array (ndarray) โ€” NumPy v2.2 Manual
The array can be indexed using Python container-like syntax: >>> # The element of x in the *second* row, *third* column, namely, 6. >>> x[1, 2] 6 ยท For example slicing can produce views of the array:
๐ŸŒ
Sdsu
gawron.sdsu.edu โ€บ python_for_ss โ€บ course_core โ€บ book_draft โ€บ data โ€บ numpy.html
6.2. More on two-dimensional arrays โ€” python_for_ss 0.1.1 documentation
In this case, though, using the array operation is about an order of magnitude less efficient than the natural Pythonic idiom. ... More on array operations and efficiency below. Itโ€™s a tricky subject. Boolean operations work more or less as expected on 2D arrays: import numpy as np y = np.arange(35).reshape(5,7) y>3