Python List comprehension and JSON parsing
<p>I'm new to Python and trying to figure out the best way to parse the values of a JSON object into an array, using a list comprehension. </p>

<p>Here is my code - I'm querying the publicly available iNaturalist API and would like to take the JSON object that it returns, so that I take specific parts of the JSON object into a bumpy array:</p>

<pre><code>import json
import urllib2

#Set Observations URL request for Resplendent Quetzal of Costa Rica
query = urllib2.urlopen("http://api.inaturalist.org/v1/observations?place_id=6924&amp;taxon_id=20856&amp;per_page=200&amp;order=desc&amp;order_by=created_at")
obSet = json.load(query)

#Print out Lat Long of observation
n = obSet['total_results']

for i in range(n) :
    print obSet['results'][i]['location'] 
</code></pre>

<p>This all works fine and gives the following output:</p>

<pre><code>9.5142456535,-83.8011438905
10.2335478381,-84.8517773638
10.3358965682,-84.9964271008
10.3744851815,-84.9871494128
10.2468720343,-84.9298072822
...
</code></pre>

<p>What I'd like to do next is replace the for loop with a list comprehension, and store the location value in a tuple. I'm struggling with the syntax in that I'm guessing it's something like this:</p>

<pre><code>[(long,lat) for i in range(n) for (long,lat) in obSet['results'][i]['location']]
</code></pre>

<p>But this doesn't work...thanks for any help.</p>

<pre><code>[tuple(obSet['results'][i]['location'].split(',')) for i in range(n)]
</code></pre>

<p>This will return a list of tuple, elements of the tuples are <code>unicode</code>.</p>

<p>If you want that the elements of tuples as floats, do the following:</p>

<pre><code>[tuple(map(float,obSet['results'][i]['location'].split(','))) for i in range(n)]
</code></pre>

python json list-comprehension