More efficient way to create JSON from Python
<p>I'd like to write an API that reads from a CSV on disk (with x, y coordinates) and outputs them in JSON format to be rendered by a web front end. The issue is that there are lots of data points (order of 30k) and so going from numpy arrays of x and y into JSON is really slow.</p>

<p>This is my current function to get the data in JSON format. Is there any way to speed this up? It seems very redundant to have such a large data structure for each 2d point.</p>

<pre><code>def to_json(xdata, ydata):
    data = []
    for x, y in zip(xdata, ydata):
        data.append({"x": x, "y": y})
    return data
</code></pre>

<p>You could use list comprehension like:</p>

<pre><code>def to_json(xdata, ydata):
    return  [{"x": x, "y": y} for x, y in zip(xdata, ydata)]
</code></pre>

<p>Eliminates use of unnessacary variable, and is cleaner.</p>

<p>You can also use generators like:</p>

<pre><code>def to_json(xdata, ydata):
    return  ({"x": x, "y": y} for x, y in zip(xdata, ydata))
</code></pre>

<p>They're created super fast and are light on the system, use little to no memory.
This last's until you do something like convert it to a list.</p>

<p>Since the objects are just x-y co-ordinates i'd use a generator object with x-y tuples - which are also created faster -  like so:</p>

<pre><code>def to_json(xdata, ydata):
    return  ((x,y) for x, y in zip(xdata, ydata))
</code></pre>

<p>Edit: You could replace the tuples with lists <code>[]</code>, theyre valid JSON arrays.</p>

python json rest