>>> import numpy as np
>>> np.array(data_list).reshape(-1, 2)
array([[0, 1],
       [2, 3],
       [4, 5],
       [6, 7],
       [8, 9]])

(The reshape method returns a new "view" on the array; it doesn't copy the data.)

Answer from Fred Foo on Stack Overflow
🌐
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
🌐
iO Flood
ioflood.com › blog › python-2d-array
Python 2D Array with Lists | Guide (With Examples)
February 10, 2024 - We’ll cover everything from the basics of multi-dimensional arrays to more advanced techniques, as well as alternative approaches. Let’s get started! To create a 2D array in Python, you can use nested lists.
Discussions

How can I turn a flat list into a 2D array in python? - Stack Overflow
Next You’ll be prompted to create an account to view your personalized homepage. Find centralized, trusted content and collaborate around the technologies you use most. Learn more about Collectives ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... Save this question. Show activity on this post. ... Closed 2 years ago. ... Save this answer. ... Show activity on this post. Copy>>> import numpy as np >>> np.array(data_list... More on stackoverflow.com
🌐 stackoverflow.com
November 15, 2020
Python - Conversion of list of arrays to 2D array - Stack Overflow
The comments and answers pointing ... tuples, lists, or np.arrays themselves) must have the same length. If they don't, you'll still get A.shape = (3,) and A will have dtype=object. I've definitely been snagged by this, when elements unexpectedly had different lengths. ... Sign up to request clarification or add additional context in comments. ... If I understood correctly what you're asking, you have a case where numpy did not convert array of arrays into 2d ... More on stackoverflow.com
🌐 stackoverflow.com
python - How to define a two-dimensional array? - Stack Overflow
You can, however, create multidimensional sequences, as the answers here show. Remember that python variables are untyped, but values are strongly typed. SingleNegationElimination – SingleNegationElimination · 2011-07-12 16:05:19 +00:00 Commented Jul 12, 2011 at 16:05 ... I'm confused. Coming from other languages: it IS a difference between an 1D-Array containing 1D-Arrays and a 2D-Array. And AFAIK there is no way of having a multi-dimensional-array (or list... More on stackoverflow.com
🌐 stackoverflow.com
python - how to convert 2d list to 2d numpy array? - Stack Overflow
the inner lists won't be converted to numpy arrays). Which totally makes sense as you cannot have a 2D array (matrix) with variable 2nd dimension. 2017-07-22T01:48:10.11Z+00:00 ... Thank you from September 2017 (Ubuntu 16.04 LTS). This is what I needed. Much simpler than anticipated. More on stackoverflow.com
🌐 stackoverflow.com
🌐
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:
🌐
CodeSpeedy
codespeedy.com › home › how to create 2d array from list of lists in python
Create 2D Array From List in Python - CodeSpeedy
April 11, 2019 - import numpy as np codespeedy_list = [[4,6,2,8],[7,9,6,1],[12,74,5,36]] codespeedy_2d_array = np.array(codespeedy_list) print(codespeedy_2d_array) ... You can write anything instead of np. But writing np is globally accepted term. Thus it will be a good practice for us to write np instead of something else. Then you can see we have taken a list, codespeedy_list · We also provided some elements in it. This type of lists is also known as a list of lists. Because in our list we have 3 lists. We have used a list of lists because our main goal is to create a 2d array.
🌐
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
Top answer
1 of 4
23

Not sure if I understood the question correctly, but does this work for you?

import numpy as np
A = [[1,2,3],[4,5,6],[7,8,9]]
A = np.array(A)

If A is a list of numpy array, how about this:

Ah = np.vstack(A)
Av = np.hstack(A)
2 of 4
8

If I understood correctly what you're asking, you have a case where numpy did not convert array of arrays into 2d array. This can happen when your arrays are not of the same size. Example:

Automatic conversion to 2d array:

import numpy as np
a = np.array([np.array([1,2,3]),np.array([2,3,4]),np.array([6,7,8])])
print a

Output:

>>>[[1 2 3]
    [2 3 4]
    [6 7 8]]

No automatic conversion (look for the change in the second subarray):

import numpy as np
b = np.array([np.array([1,2,3]),np.array([2,3,4,5]),np.array([6,7,8])])
print b

Output:

>>>[array([1, 2, 3]) array([2, 3, 4, 5]) array([6, 7, 8])]

I found a couple of ways of converting an array of arrays to 2d array. In any case you need to get rid of subarrays which have different size. So you will need a mask to select only "good" subarrays. Then you can use this mask with list comprehensions to recreate array, like this:

import numpy as np

a = np.array([np.array([1,2,3]),np.array([2,3,4,5]),np.array([6,7,8])])
mask = np.array([True, False, True])

c = np.array([element for (i,element) in enumerate(a) if mask[i]])

print a
print c

Output:

>>>>[array([1, 2, 3]) array([2, 3, 4, 5]) array([6, 7, 8])]
>>>>[[1 2 3]
     [6 7 8]]

Or you can delete "bad" subarrays and use vstack(), like this:

import numpy as np

a = np.array([np.array([1,2,3]),np.array([2,3,4,5]),np.array([6,7,8])])
mask = np.array([True, False, True])

d = np.delete(a,np.where(mask==False))
e = np.vstack(d)

print a
print e

Output:

>>>>[array([1, 2, 3]) array([2, 3, 4, 5]) array([6, 7, 8])]
>>>>[[1 2 3]
     [6 7 8]]

I believe second method would be faster for large arrays, but I haven't tested the timing.

Find elsewhere
🌐
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.
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.

🌐
Processing
py.processing.org › tutorials › 2dlists
Two-Dimensional Lists \ Tutorials
Python Mode for Processing extends the Processing Development Environment with the Python programming language.
🌐
Dot Net Perls
dotnetperls.com › 2d-python
Python - 2D List Examples - Dot Net Perls
To construct a 2D list, we can use append() or an initializer. We create an empty list and add empty lists to it with append(). We can construct any rectangular (or jagged) list this way. In this example we build a 2 by 2 list. Step 1 We first create an empty list with the empty square brackets.
🌐
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.
🌐
Guru99
guru99.com › home › python › python 2d arrays: two-dimensional list examples
Python 2D Arrays: Two-Dimensional List Examples
October 7, 2026 - Use a list comprehension such as [[0]*cols for _ in range(rows)]. Avoid [[0]*cols]*rows, because it repeats the same inner list reference, so editing one row accidentally changes every row.
🌐
MLJAR
mljar.com › answers › define-two-dimensional-array-python
Define two-dimensional array in Python
Now, you can add items to the list: array[0][0] = 1 array[2][5] = 9 · REMEMBER Be careful with idexing, you can't go out of range! 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.
🌐
PyTutorial
pytutorial.com › python-2d-arrays-guide-lists-numpy-examples
PyTutorial | Python 2D Arrays Guide: Lists, NumPy, Examples
March 25, 2026 - Python does not have a built-in 2D array type. But we can create them easily. The most common ways are using nested lists or the NumPy library.
🌐
Board Infinity
boardinfinity.com › blog › python-2d-array
Python 2D Array | Board Infinity
August 13, 2025 - ... In the code above, we multiply the array with the columns ©, now this will result in a 1D list, whose size will be equal to the number of columns. After it, we again multiply the array with the rows ®, again this will result in the ...
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › 2d array in python
Comprehensive Guide to 2D Arrays in Python: Creation, Access, and Use
November 11, 2024 - They are useful when initializing a 2D array with specific values or performing transformations on its elements. Example: Creating a 3x3 identity matrix using list comprehension ... NumPy is a powerful library for numerical computing in Python. It offers efficient data structures and functions for working with arrays, including 2D arrays.