Search code examples
python-3.xnumpynumpy-ndarraydeque

numpy: create deque from ndarray which is an array of arrays


I have a numpy ndarray in this form:

inputs = np.array([[1],[2],[3]])

How can I convert this ndarray to a deque (collections.deque) so that the structure get preserved (array of arrays) and I could apply normal deque methods such as popleft() and append()? for example:

inputs.popleft()
->>> [[2],[3]]

inputs.append([4])
->>> [[2],[3], [4]]

Solution

  • Hmm I think that you can do:

    inputs = np.array([[1],[2],[3]])
    inputs = collections.deque([list(i) for i in inputs])
    inputs.append([4])
    inputs.popleft()
    

    EDIT. I edited code