vinay hegde vinay hegde - 1 year ago 100
Python Question

Removing duplicates(non-exact) tuples from a list in python

Consider a list of tuples:

[(14,1), (10,5), (8,7), (1,14)]

I need to eliminate one of
. So the expected result should be

Note: Order doesn't matter.

I'm aware that
can be used to eliminate exact duplicates. Also combinations would not be of much help too because all of them add up to the same total.

Answer Source

Given the scenario, it would be wise to instead use a set of sets. Sets do not allow duplicates, and order was specified to not matter.

If you have no control of the creation of the data and instead must convert from your current data to this, you can this to convert your current data:

{frozenset(i) for i in dataset}

You have to use frozenset as set is not a hashable type. This is no more restrictive than using a tuple.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download