I have written the following code which takes a
coord_list of points in a 2D coordinate system, a
center and a
radius and returns a list of all points from the list having distance at most
def points_in_rad(coord_list, center, radius): distances = [distance(coord, center) for coord in coord_list] result = [coord_list[i] for i in range(len(coord_list)) if distances[i] <= radius] return result
I am utilising here a function
distance computing the distance between two points. The code works well but I am expecting
coord_list to contain many points, say 10k or more. Performing two list comprehensions is thus quite slow and requires some memory and I would like to speed up this function and at the same time reduce the memory requirement. My idea was to use generators. For example, I could just write
distances = (distance(coord, center) for coord in coord_list)
but I'm struggling with converting the second list comprehension into a generator also as this requires looking up
distances at a specific index.
Any ideas on how this can be done or other improvements to the code?