I have a really large numpy of array of lists, and I want to append an element to each of the arrays. I want to avoid using a loop for the sake of performance.
The following syntax is not working.
a=np.array([[],[1],[0,1]])[j.append(3) for j in a]
The expected result for this example would be
np.array([[3],[1,3],[0,1,3])
but I got
[None, None, None]
Any clue?
list.append()
returns None
, so you're just seeing that the 3 append operations each returned None
.
However, if you print the contents of a
you'll find that the appends were successful.
a=np.array([[],[1],[0,1]])
[j.append(3) for j in a]
print(a)
# prints "[list([3]) list([1, 3]) list([0, 1, 3])]"
I wouldn't recommend using a numpy array of lists for anything, but maybe you have some special use case where this makes sense. I suggest asking a separate quesiton that looks at the reasoning for using a numpy array of lists