Note that there are some explanatory texts on larger screens.

plurals
  1. POPython IndexError: list index out of range doesn't make sense
    primarykey
    data
    text
    <p>I am working on a little python code which parses the response from the google maps API, however I get a "IndexError: list index out of range" error, which does make any sense to me. For some requests, the code is working, for some others, not.</p> <p>Purpose of the code is to structure the address response to a random address and put in a dict structure for a later use.</p> <p>I checked for the range of the list (e.g. counting until n-1), but it does not seem to be an issue. </p> <pre><code>Exact traceback: Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; File "geoCodeAPI.py", line 67, in getLocation data[i] = {'formatted_address': formattedAddress[i], 'street': streetname[i], 'street_number': streetnumber[i], 'city': city[i], 'country': country[i], 'lat': lat[i], 'lng': lng[i]} IndexError: list index out of range </code></pre> <p>Any suggestions and general comments to the code are highly appreciated. Thank you.</p> <pre><code>#!/usr/bin/python import urllib import json jsonResponse = '' def getLocation(rawAddressString): addressString = rawAddressString.replace(" ", "+"); url = "http://maps.googleapis.com/maps/api/geocode/json?address=" + addressString+ "&amp;sensor=false"; googleResponse = urllib.urlopen(url); result = json.load(googleResponse) formattedAddress = [] lat = [] lng = [] country = [] streetnumber = [] streetname = [] city = [] for s in result['results']: formattedAddress.append(s['formatted_address']) lat.append(s['geometry']['location']['lat']) lng.append(s['geometry']['location']['lng']) for foo in s['address_components']: if foo['types'][0] == "country": country.append(foo['long_name']) else: country.append("NA") if foo['types'][0] == "street_number": streetnumber.append(foo['long_name']) else: streetnumber.append("NA") if foo['types'][0] == "route": streetname.append(foo['long_name']) else: streetname.append("NA") if "locality" in foo['types'][0]: city.append(foo['long_name']) else: city.append("NA") data = dict() print range(len(country)) print range(len(city)) for i in (range(len(country))): data[i] = {'formatted_address': formattedAddress[i], 'street': streetname[i], 'street_number': streetnumber[i], 'city': city[i], 'country': country[i], 'lat': lat[i], 'lng': lng[i]} return data </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload