Iterate through a List and Remove One Item Until List is Empty


I am having issues iterating through a list in Dynamo. I have a user selected start point that I would like to compare to a list of points to find the closest point within that list. Then I would like to take that 2nd point and compare it to the remaining points to find the closest point to the 2nd point and so on until the list of points is empty. Currently, I only know how to do this by copying the same nodes over, but I’m sure there is a more efficient way to do this. Any help is appreciated! Thanks in advance.

Right now my group of nodes to be repeated is:
Geometry.DistanceTo -> List.MinimumItem -> List.IndexOf -> List.RemoveItemAtIndex

You will have to use a for loop:

Give it a try and let us know how it goes.

1 Like

is there a reason you need to remove them from the list? i’m not sure if I completely understand what you’re trying to do, but your output should be 2 points? the point closest to the user point (p1), the point nearest that (p2) and the next point nearest p2?

or are you trying to create a list of shortest distances between the collection of points (p0, p1, … pn)?

The reason I would like to remove it from the list is so it is not included when checking the closest point to the following point. I’m trying to label points from 1 to n by distance from the previous point, but there may be a case in which point 15 is closer to point 1 than point 10 is (see screenshot below). I would like to start with point 1, find the closest point (2), and then compare point 2 to the remaining points (excluding 1) to find point 3 and so on.


Thank you! I will check these out and let you know if I get a solution.