Search code examples
pythonflaskdeque

How to send a deque collection via flask in python


I am trying to send a deque collection filled with integers via Flask in a simple flask app.

the problem I am facing is that the deque is not serializable

from flask import Flask, jsonify
import collections


d=collections.deque(maxlen=10)


app=Flask(__name__)
@app.route('/')

def index():
    for i in range(10):
        d.append(i)
    return jsonify(d)

if __name__=='__main__':
    app.run(debug=True)

the output error is

TypeError: Object of type deque is not JSON serializable

How can i serialize a deque collection to be sent via flask? taking into consideration that I need to undergo some mathimatical algorithms over the sent integers through this deque on another python server.


Solution

  • Python deque is similar to list (in many terms). The main difference I know between both, is that changing operations (such as pop()) are way more efficiant with deque.

    So you don't you try to jsonify a list instead of a deque ?