my_list = [[1,2,2,1], [0,0,1,2], [1,2,0,0], [1,0,0,1]]
my_list =[np.trim_zeros(np.array(a)) for a in my_list]
>>> my_list
[array([1, 2, 2, 1]), array([1, 2]), array([1, 2]), array([1, 0, 0, 1])]
If you want numpy.
Can also just do:
>>> my_list =[np.trim_zeros(a) for a in my_list]
>>> my_list
[[1, 2, 2, 1], [1, 2], [1, 2], [1, 0, 0, 1]]
Some timings:
Numpy
>>> timeit.timeit('my_list =[np.trim_zeros(a) for a in my_list]',setup='import numpy as np; my_list = [[1,2,2,1], [0,0,1,2], [1,2,0,0], [1,0,0,1]]', number=10000)
0.08429217338562012
Numpy w/convert array
>>> timeit.timeit('my_list =[np.trim_zeros(np.array(a)) for a in my_list]',setup='import numpy as np; my_list = [[1,2,2,1], [0,0,1,2], [1,2,0,0], [1,0,0,1]]', number=10000)
0.6929900646209717
So really best off not to convert at np.array unless you are going to use that type later.