Hi all,
I have a list of ordered pair tuples which will be used as verticies in another script, the problem is that the list has repeated ordered pairs. I would like to remove the duplicates, but I cannot seem to find an algorithm, or figure out how to make one, which can detect duplicate tuple objects.
does anyone know how to do this?
thanks in advance!
Off the top of my head, here’s how I’d do it:
# Given list of ordered pairs -> old
def check_list(old):
new = []
for n in old:
if not n in new:
new.append(n)
return new
A quick google search reveals many ways of doing this, and some profiling. (Link.) It appears that this is the slowest possible way to do it. One of the faster ways is to convert the list to a set, and back again if you actually need a list:
list(set(old))
The first bit reads better, its intent is clear. However, piping the list through the set type is about 70 times faster, because you’re outsourcing the work to compiled C code, but it’s a bit obtuse. Another downside of the set approach is that it doesn’t preserve the order of the original list.
odd… I must have had something wrong elsewhere, because last time I tried this approach (and the ones on the website you cited), I received an error that type “list” was not hashable. It’s working now, thanks for your help.