1

I have a numpy array, which might look something like this

[[1 2]
 [3 4]]

I would like to convert it to have one extra dimension, as such:

[[[1] 
  [2]]
 [[3]
  [4]]]

Is there a simple way to do this?

Kai
  • 213
  • 1
  • 12

1 Answers1

2

Use np.newaxis and tolist:

a = np.array([[1, 2], [3, 4]])
a[:, :, np.newaxis].tolist()

Output:

[[[1], [2]], [[3], [4]]]
gmds
  • 19,325
  • 4
  • 32
  • 58