How do I convert a simple list of lists into a numpy array? The rows are individual sublists and each row contains the elements in the sublist.
9 Answers
If your list of lists contains lists with varying number of elements then the answer of Ignacio Vazquez-Abrams will not work. Instead there are at least 3 options:
1) Make an array of arrays:
x=[[1,2],[1,2,3],[1]]
y=numpy.array([numpy.array(xi) for xi in x])
type(y)
>>><type 'numpy.ndarray'>
type(y[0])
>>><type 'numpy.ndarray'>
2) Make an array of lists:
x=[[1,2],[1,2,3],[1]]
y=numpy.array(x)
type(y)
>>><type 'numpy.ndarray'>
type(y[0])
>>><type 'list'>
3) First make the lists equal in length:
x=[[1,2],[1,2,3],[1]]
length = max(map(len, x))
y=numpy.array([xi+[None]*(length-len(xi)) for xi in x])
y
>>>array([[1, 2, None],
>>> [1, 2, 3],
>>> [1, None, None]], dtype=object)
-
2`dtype=float` works too, it will convert `None` to `np.nan`, which may be useful. – May 22 '20 at 12:08
-
On python 3.9, I had to use `(np.vectorize(len)(x)).max()` instead of `max(map(len, x))` on the third solution – Begoodpy Jun 03 '21 at 06:46
-
6And you get a warning: `VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray return array(a, dtype, copy=False, order=order)` – Begoodpy Jun 03 '21 at 06:47
>>> numpy.array([[1, 2], [3, 4]])
array([[1, 2], [3, 4]])

- 6,248
- 4
- 19
- 37

- 776,304
- 153
- 1,341
- 1,358
-
19this automatically convert a list of list in a 2D array because the length of all included lists are the same. Do you know how not to do that: make an array of list even if all the lists have the same length? Or is it possible to convert a 2D array in a 1D array of 1D array (efficiently I mean, no iterative method or python map stuff) – Juh_ Oct 04 '12 at 09:58
-
9If that doesn't work for you because your sublists are not of even sizes, see [the following answer](http://stackoverflow.com/a/26224619/1449460). – Nikana Reklawyks Oct 17 '16 at 05:27
-
@NikanaReklawyks I was confused after looking at the answer but your comment was helpful. I found out that my list of lists was jagged, when it wasn't supposed to be. – Nikhil Girraj Dec 21 '19 at 05:25
-
How fast is this with respect to the length of the argument? I am not seeing a good answer in the documentation? – Czarking Aug 05 '20 at 00:54
As this is the top search on Google for converting a list of lists into a Numpy array, I'll offer the following despite the question being 4 years old:
>>> x = [[1, 2], [1, 2, 3], [1]]
>>> y = numpy.hstack(x)
>>> print(y)
[1 2 1 2 3 1]
When I first thought of doing it this way, I was quite pleased with myself because it's soooo simple. However, after timing it with a larger list of lists, it is actually faster to do this:
>>> y = numpy.concatenate([numpy.array(i) for i in x])
>>> print(y)
[1 2 1 2 3 1]
Note that @Bastiaan's answer #1 doesn't make a single continuous list, hence I added the concatenate
.
Anyway...I prefer the hstack
approach for it's elegant use of Numpy.

- 983
- 9
- 12
-
22while some people may be looking for this, I'm pretty sure the OP wanted a multi-dimensional nparr. – Nathan majicvr.com Jun 08 '18 at 19:18
It's as simple as:
>>> lists = [[1, 2], [3, 4]]
>>> np.array(lists)
array([[1, 2],
[3, 4]])

- 28,332
- 6
- 65
- 82
Again, after searching for the problem of converting nested lists with N levels into an N-dimensional array I found nothing, so here's my way around it:
import numpy as np
new_array=np.array([[[coord for coord in xk] for xk in xj] for xj in xi], ndmin=3) #this case for N=3

- 91
- 1
- 3
-
Note that if you already have the nested-lists structure, you don't need the `[...[...[...]]]` part. You just need to call `np.array`, with `ndmin=number-of-list-layers`. (though in my case I needed `ndmin=number-of-list-layers-minus-1` for some reason, else created an extra layer -- need to investigate) – Venryx May 19 '20 at 03:50
-
Ah okay, the problem in my case is that for the deepest "list layer", the lists did not all have the same length, which caused `np.array` to just "wrap" those deepest-lists rather than convert them into numpy arrays. – Venryx May 19 '20 at 04:34
-
`xi = [[[3, 4], [3, 4]], [[3, 4], [3, 4]]]` gives `array([[[3, 4], [3, 4]], [[3, 4], [3, 4]]])`. I only see the array at the outer layer here. I thought the question is about converting everything to a numpy array? Or did I choose a wrong example? – questionto42 Jun 02 '21 at 18:53
The OP specified that "the rows are individual sublists and each row contains the elements in the sublist".
Assuming that the use of numpy
is not prohibited (given that the flair numpy has been added in the OP), use vstack
:
import numpy as np
list_of_lists= [[1, 2, 3], [4, 5, 6], [7 ,8, 9]]
array = np.vstack(list_of_lists)
# array([[1, 2, 3],
# [4, 5, 6],
# [7, 8, 9]])
or simpler (as mentioned in another answer),
array = np.array(list_of_lists)

- 30,938
- 9
- 118
- 133
As mentioned in the other answers, np.vstack()
will let you convert your list-of-lists(nested list) into a 1-dimensional array of sublists. But if you are looking to convert the list of lists into a 2-dimensional numpy.ndarray. Then you can use the numpy.asarray()
function.
For example, if you have a list of lists named y_true
that looks like:
[[0, 1, 0], [1, 0, 0], [0, 0, 1], [1, 0, 0], [0, 1, 0], [0, 0, 1], [1, 0, 0]]
<class 'list'>
This line y_true = np.asarray(y_true)
will convert the list of lists into a 2-dimensional numpy ndarray that looks like:
[[0 1 0]
[1 0 0]
[0 0 1]
[1 0 0]
[0 1 0]
[0 0 1]
[1 0 0]]
<class 'numpy.ndarray'>
Additionally, you can also specify the dtype
parameter like np.asarray(y_true, dtype = float)
to have your array values in your desired data type.

- 541
- 5
- 19
I had a list of lists of equal length. Even then Ignacio Vazquez-Abrams
's answer didn't work out for me. I got a 1-D numpy array whose elements are lists. If you faced the same problem, you can use the below method
Use numpy.vstack
import numpy as np
np_array = np.empty((0,4), dtype='float')
for i in range(10)
row_data = ... # get row_data as list
np_array = np.vstack((np_array, np.array(row_data)))

- 6,407
- 8
- 44
- 87
-
2why on earth would you keep stacking if you know that you have 10 lists, why not np.empty((10, 4)) and then just filling it up? – Mehdi Aug 20 '19 at 07:32
Just use pandas
list(pd.DataFrame(listofstuff).melt().values)
this only works for a list of lists
if you have a list of list of lists you might want to try something along the lines of
lists(pd.DataFrame(listofstuff).melt().apply(pd.Series).melt().values)

- 105
- 1
- 8