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)
Answer from YS-L 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
Top answer
1 of 4
22

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.

๐ŸŒ
Snakify
snakify.org โ€บ two-dimensional lists (arrays)
Two-dimensional lists (arrays) - Learn Python 3 - Snakify
But the easiest way is to use generator, creating a list of n elements, each of which is a list of m zeros: ... In this case each element is created independently from the others. The list [0] * m is n times consructed as the new one, and no copying of references occurs. Say, a program takes on input two-dimensional array in the form of n rows, each of which contains m numbers separated by spaces.
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Convert 1D Array to 2D Array in Python (numpy.ndarray, list) | note.nkmk.me
May 15, 2023 - l = [0, 1, 2, 3, 4, 5] print(np.array(l).reshape(-1, 3).tolist()) # [[0, 1, 2], [3, 4, 5]] print(np.array(l).reshape(3, -1).tolist()) # [[0, 1], [2, 3], [4, 5]] ... See the following article on how to convert numpy.ndarray and list to each other. ... If NumPy is not available, you can still achieve the transformation using list comprehensions, range(), and slices. ... def convert_1d_to_2d(l, cols): return [l[i:i + cols] for i in range(0, len(l), cols)] l = [0, 1, 2, 3, 4, 5] print(convert_1d_to_2d(l, 2)) # [[0, 1], [2, 3], [4, 5]] print(convert_1d_to_2d(l, 3)) # [[0, 1, 2], [3, 4, 5]] print(convert_1d_to_2d(l, 4)) # [[0, 1, 2, 3], [4, 5]]
๐ŸŒ
Finxter
blog.finxter.com โ€บ python-list-to-2d-array-the-ultimate-conversion-guide
Python List to 2D Array โ€“ The Ultimate Conversion Guide โ€“ Be on the Right Side of Change
List comprehension is a powerful and concise way to create lists in Python. You can use list comprehension to create 2D arrays by combining two or more list comprehensions.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to convert a list to a 2d array?
r/learnpython on Reddit: How to convert a list to a 2D array?
May 5, 2020 -

I have a list:

data=[1,2,3,4,5,6,7,...]

and I want to transform it into a 2-dimensional array, with 5 columns and 10 rows

How can I do it?

Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-using-2d-arrays-lists-the-right-way
Python | Using 2D arrays/lists the right way - GeeksforGeeks
The code then shows another approach using a nested list comprehension to create the 2D array arr. This method avoids aliasing by creating a new list for each row, resulting in a proper 2D array. ... # Python 3 program to demonstrate working # of method 1 and method 2.
Published ย  June 20, 2024
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ several lists into one 2d matrix
r/learnpython on Reddit: several lists into one 2d matrix
August 15, 2023 -

Hello, I have a simple question : For example, I have 3 lists a,b,c , and I want to join them into one big 2d array called d, how do I do it:

a= [1,2,3]

b= [4,5,6]

c= [7,8,9]

result wanted :

d= [

[1,2,3],

[4,5,6],

[7,8,9]

]

thank you !!!

๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ numpy โ€บ convert-a-nested-python-list-to-a-2d-numpy-array-and-print.php
Convert a nested Python list to a 2D NumPy array and print
Convert to 2D NumPy Array: Use np.array() to convert the nested list into a 2D NumPy array. Print 2D Array: Output the resulting 2D NumPy array to verify the conversion. ... Write a Numpy program to convert a nested Python list with inconsistent ...
๐ŸŒ
Guru99
guru99.com โ€บ home โ€บ python โ€บ python 2d arrays: two-dimensional list examples
Python 2D Arrays: Two-Dimensional List Examples
August 12, 2024 - Here are two methods for updating values in the 2-D array(list). ... #creare 2D array with 4 rows and 5 columns array=[[23,45,43,23,45],[45,67,54,32,45],[89,90,87,65,44],[23,45,67,32,10]] #update row values in the 3rd row array[2]=[0,3,5,6,7] #update row values in the 5th row array[2]=[0,3,5,6,7] #update the first row , third column array[0][2]=100 #update the second row , third column array[1][2]=400 #display print(array)
Top answer
1 of 3
7

If your lists are NOT of the same length (in each nested dimension) you CANT do a traditional conversion to a NumPy array because it's necessary for a NumPy array of 2D or above to have the same number of elements in its first dimension.

So you cant convert [[1,2],[3,4,5]] to a numpy array directly. Applying np.array will give you a 2 element numpy array where each element is a list object as - array([list([1, 2]), list([3, 4, 5])], dtype=object). I believe this is the issue you are facing.

You cant create a 2D matrix for example that looks like -

[[1,2,3,?],
 [4,5,6,7]]

What you may need to do is pad the elements of each list of lists of lists to a fixed length (equal lengths for each dimension) before converting to a NumPy array.

I would recommend iterating over each of the lists of lists of lists as done in the code I have written below to flatten your data, then transforming it the way you want.


If your lists are of the same length, then should not be a problem with numpy version 1.18.5 or above.

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

       [[5, 6],
        [7, 8]]])

However, if you are unable to still work with the list of list of lists, then you may need to iterate over each element first to flatten the list and then change it into a numpy array with the required shape as below -

a = [[[1,2],[3,4]],[[5,6],[7,8]]]
flat_a = [item for sublist in a for subsublist in sublist for item in subsublist]
np.array(flat_a).reshape(2,2,2)
array([[[1, 2],
        [3, 4]],

       [[5, 6],
        [7, 8]]])
2 of 3
0

Try this:

>>> import numpy as np
>>> a = np.array([[[1,2],[3,4],[5,6]],[[7,8],[9,10],[11,12]]])
>>> a
array([[[ 1,  2],
        [ 3,  4],
        [ 5,  6]],

       [[ 7,  8],
        [ 9, 10],
        [11, 12]]])
>>> a.reshape(4,-1)
array([[ 1,  2,  3],
       [ 4,  5,  6],
       [ 7,  8,  9],
       [10, 11, 12]])


>>> a.reshape(1,-1)
array([[ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12]])
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ python-2d-array
Python 2D Array with Lists | Guide (With Examples)
February 10, 2024 - Each inner list [1, 2, 3], [4, 5, 6], and [7, 8, 9] represents a row in the 2D array. When we print the array, we get the output as a nested list, which is the Pythonic way of representing a 2D array. This is a basic way to create a 2D array in Python, but thereโ€™s much more to learn about ...
๐ŸŒ
NumPy
numpy.org โ€บ devdocs โ€บ user โ€บ absolute_beginners.html
NumPy: the absolute basics for beginners โ€” NumPy v2.5.dev0 Manual
The four values listed above correspond to the number of columns in your array. With a four-column array, you will get four values as your result. Read more about array methods here. You can pass Python lists of lists to create a 2-D array (or โ€œmatrixโ€) to represent them in NumPy.
๐ŸŒ
Processing
py.processing.org โ€บ tutorials โ€บ 2dlists
Two-Dimensional Lists \ Tutorials
Python Mode for Processing extends the Processing Development Environment with the Python programming language.
๐ŸŒ
Finxter
blog.finxter.com โ€บ 5-best-ways-to-convert-a-python-list-of-tuples-to-a-2d-array
5 Best Ways to Convert a Python List of Tuples to a 2D Array โ€“ Be on the Right Side of Change
The map() function here creates an iterator that applies list to each tuple in tuples_list, and then we use the list() function to convert this iterator back into a list, yielding the 2D array.
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.

๐ŸŒ
Dot Net Perls
dotnetperls.com โ€บ 2d-python
Python - 2D List Examples - Dot Net Perls
Version 2 This code accesses the flattened list, using an expression to compute the correct index. Result Accessing the element at position coordinates 1, 2 is slightly faster in the flattened list. import time # Nested, 3x2. nested_list = [] nested_list.append([ ... Python supports a special "array" from the array module. An integer array is more compact in memory than an integer list. We can create a flattened 2D array.