List of lists into numpy array
PythonListNumpyPython Problem Overview
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.
Python Solutions
Solution 1 - Python
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:
-
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(y[0]) >>> -
Make an array of lists:
x=[[1,2],[1,2,3],[1]] y=numpy.array(x) type(y) >>>
type(y[0]) >>> -
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)
Solution 2 - Python
>>> numpy.array([[1, 2], [3, 4]])
array([[1, 2], [3, 4]])
Solution 3 - Python
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.
Solution 4 - Python
It's as simple as:
>>> lists = [[1, 2], [3, 4]]
>>> np.array(lists)
array([[1, 2],
[3, 4]])
Solution 5 - Python
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
Solution 6 - Python
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)
Solution 7 - Python
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)))
Solution 8 - Python
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)