Suppose you have a variable length list and you want to convert it to a numb array
alist = [[1,2,3],[5,6]]
What is the efficient way to convert this list to a numpy array?
My first answer was using pandas and this is what I did?
import pandas as pd data = pd.Dataframe(alist).fillna(0).values
This worked and I moved on to my other problem, but then realised if there is any other way which is more efficient. Turns out there is.
import itertools data=np.array(list(itertools.izip_longest(*alist, fillvalue=0))).T
In python 2.7 and the following in python3
import itertools data=np.array(list(itertools.zip_longest(*alist, fillvalue=0))).T
How Fast and efficient? See the below image.
Post writing the above I googled and found this link. Here is the result of both of the methods on the example data in the link.
Clearly, itertools is the winner.