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
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.

๐ŸŒ
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]]
Discussions

How can I turn a flat list into a 2D array in python? - Stack Overflow
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).reshape(-1, 2) ... 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. ... 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
September 26, 2015
How to convert a list to a 2D array?
Look up numpy More on reddit.com
๐ŸŒ r/learnpython
9
1
May 5, 2020
python - Converting a list of lists into a 2D numpy array - Stack Overflow
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. More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
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?

๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ 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
October 27, 2023 - To convert a 2D list into a 2D array in Python, you can use the NumPy library. First, import NumPy by adding import numpy as np at the beginning of your code.
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]])
Find elsewhere
๐ŸŒ
APXML
apxml.com โ€บ courses โ€บ essential-numpy-pandas โ€บ chapter-2-getting-started-numpy-arrays โ€บ creating-arrays-from-lists
NumPy Array from Python List
You can create multi-dimensional arrays by passing nested Python lists (lists containing other lists) to np.array(). # A nested Python list (list of lists) nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] print(f"Original nested list:\n{nested_list}") # Convert to a 2D NumPy array numpy_2d_array = np.array(nested_list) print(f"Converted 2D NumPy array:\n{numpy_2d_array}") print(f"Array shape: {numpy_2d_array.shape}") # Output: (3, 3) -> 3 rows, 3 columns print(f"Array dimensions: {numpy_2d_array.ndim}") # Output: 2 print(f"Array dtype: {numpy_2d_array.dtype}") # Output: int64 (usually)
๐ŸŒ
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.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ python-convert-1d-list-to-2d-list-of-variable-length
Convert 1D List to 2D List of Variable Length in Python
from itertools import islice # Given list listA = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'] # Length of 2D lists needed len_2d = [ 3, 2] # Use islice def convert(listA, len_2d): res = iter(listA) return [list(islice(res,i)) for i in len_2d] res = [convert(listA, len_2d)] print("The new 2D lis is: \n",res)
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-convert-1d-list-to-2d-list-of-variable-length
Convert 1D list to 2D list of variable length- Python - GeeksforGeeks
July 11, 2025 - If we need to dynamically partition a 1D list into smaller sublists, deque from the collections module allows for efficient removal of elements from the front. popleft() is optimized for constant-time removals, which makes it a great option for partitioning a list while modifying its contents. This approach is particularly useful for real-time data processing or scenarios where data chunks are being processed and removed dynamically. ... from collections import deque a = [1, 2, 3, 4, 5, 6] b = [1, 2, 3] # Defines the variable lengths of sublists dq = deque(a) # Convert list 'a' to a deque res = [ [dq.popleft() for _ in range(size)] for size in b ] print(res)
๐ŸŒ
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
Define Nested List: Create a nested Python list where each sublist represents a row of the 2D array. Convert to 2D NumPy Array: Use np.array() to convert the nested list into a 2D NumPy array.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ combine-two-flat-lists-into-a-2d-array
Combine Two Flat Lists into a 2D Array - GeeksforGeeks
July 23, 2025 - Result is a 2D list with paired elements, and None for missing values. Using loop combines two lists by iterating through each element and appending pairs. It's flexible but less efficient compared to methods like zip() or NumPy.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ convert-python-nested-lists-to-multidimensional-numpy-arrays
Convert Python Nested Lists to Multidimensional NumPy Arrays - GeeksforGeeks
July 15, 2025 - Initialize the nested 4-dimensional list and then use numpy.asarray() function to convert the list to the array and store it in a different object.
๐ŸŒ
Codemia
codemia.io โ€บ home โ€บ knowledge hub โ€บ list of lists into numpy array
List of lists into numpy array | Codemia
September 24, 2025 - Converting a Python list of lists into a NumPy array is done with np.array(). When all inner lists have the same length, the result is a regular 2D array....