 # Caching error

I am trying to cache a function and I’m getting an error that is pointing towards these lines of code:

``````result = [[geopy.distance.geodesic(coords1, coords2).km for coords1 in list2] for coords2 in list1]

filtered_inside_radius = [[1 if y <= radius and y > 0 else 0 for y in x] for x in result]
``````

The code calculates the distance between points in two lists and then filters the results based on “radius” which is a streamlit slider.

I don’t see anything that would obviously be causing an error. Any thoughts?

Still stuck on this. The more precise error that is being generated is:

``````UserHashError: 'coords2'
``````

I’ve since learned that lists are not hashable so I’ve converted them to the variables to tuples but it is still not working:

``````    result = tuple([[geopy.distance.geodesic(coords1, coords2).km for coords1 in list2] for coords2 in
list1])

filtered_inside_radius = tuple([[1 if y <= radius and y > 0 else 0 for y in x] for x in result])``````

Okay I figured it out. But what I don’t understand is why it works. Changing the code to the below allowed me to successfully cache:

``````result = []

for coords2 in list1:
results2 = []
for coords1 in list2:
distance = geopy.distance.geodesic(coords1,coords2).km
results2.append(distance)
result.append(results2)

result = list([[geopy.distance.geodesic(coords1, coords2).km for coords1 in list2] for coords2 in list1])

for x in result:
results4 = []
for y in x:
if y <= radius and y > 0:
y = 1
results4.append(y)
else:
y = 0
results4.append(y)