Delete duplicated points in ghpython

So suppose I have a list of points like shown in the picture, how can I remove the duplicated points in ghpython (by “remove duplicates”, it means when one points appeared twice, both of them should be deleted), is there anybody know something about this?

If you use the cull duplicate points components, it has an option to average, leave one, or remove them. Should be the same in node in code.

Maybe you could do something like this:

for i in x:
    if i in x: x.remove(i)

a = x

You could also use Rhino.Point3d.CullDuplicates directly:

1 Like

@AndersDeleuran: It doesnt look like this removes ALL duplicates either though.

Could you please tell me what does "if i in x: x.remove(i) " mean?

Can you please tell me hoe can I do “average, leave one, or remove” in code, I do “ghc.CullDuplicates(x,0.01,Average)”, but it doesn’t run

Actually, I just realised I was a little quick with that one, and its not quite working as I intended.

Here is another go:

pnts = list(x)

for i in pnts:
    for j in pnts:
        if i == j: x.remove(i); x.remove(j)

a = x

There is probably a more efficient way to do it.

1 Like

Ah doh, I misunderstood the actual problem (on paternity leave with a sick child :face_with_spiral_eyes:). A strategy I often use in such cases is to implement the Counter class from collections. See the bottom GHPython component here: (6.6 KB)

1 Like

Thats a way better solution than mine. I tested it on 10k points, and I get 21.5s vs 105ms for yours. Quite the saving. Good to know.

@AndersDeleuran. Thats the second time you have helped me this week, via someone elses posts (the other one was the Mesh from Polyline solution). Great stuff!


Aww that’s nice, cheers. The native collections and itertools modules are definitely worth keeping in mind. Very useful and often quite optimised (i.e. as compared to a normal Python looped solution).

1 Like

Sorry, I never use node in code.

thanks so much! looks amazing!

1 Like

A slightly cleaner version would be to unpack the point and count variables directly, like so:

from collections import Counter

CP = []
for pt,n in Counter(P).iteritems():
    if n == 1: