TypeError: 'float' object is not subscriptable

I’m trying to return the distance between two point but I’m getting this error:

TypeError: ‘float’ object is not subscriptable

routes = directions(client, coords)

for x in routes.values():
    distance = int(int(x[0]['summary']['distance'])/1000)


The output of the routes shows that distance is basically a float number and not a list.
{‘routes’: [{‘summary’: {‘distance’: 24836.2, ‘duration’: 1849.1},


this is a problem with your dict/list iteration and access by index.
If what you posted below the code is indeed the content of the routes variable, this code should work.
Please provide a more complete code example for further analysis.

Best regards

This is a complete code:

from geopy.geocoders import Nominatim
import openrouteservice
from openrouteservice.directions import directions

locator = Nominatim(user_agent='myGeocoder')

tco_loc = locator.geocode('Svaneviksveien 29I, 5063 Bergen')
user_loc = locator.geocode('Vestlivegen 7B, 5260 Indre Arna')

tco_loc = (tco_loc.longitude, tco_loc.latitude)
user_loc = (user_loc.longitude, user_loc.latitude)

coords = (tco_loc, user_loc)

client = openrouteservice.Client(key='')
routes = directions(client, coords)

for x in routes.values():
    distance = int((x[0]['summary']['distance'])/1000)


If I print routes it works if I iterate over dictionary it doesn’t work


the problem is that the routes dict contains more than just ‘routes’:

>>> len(routes)

Thus, you’re not running into an error with the value of the routes-key, but with following values.
Pull the print()-statement into your loop and you’ll see what I mean.

Best regards

1 Like

got it thanks

distance_km = int((routes['routes'][0]['summary']['distance'])/1000)

1 Like