# Python – Remove Equilength and Equisum Tuple Duplicates

Sometimes, while working with Python tuples, we can have a problem in which we need to remove duplicates on basis of equal length and equal sum. This kind of problem can also be broken to accomodate any one of the required condition. This kind of problem can occur in data domains and day-day programming. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [(1, 2, 0), (3, 0), (2, 1)]
Output : [(1, 2, 0), (3, 0)]

Input : test_list = [(1, 2, 0), (3, 0, 0), (0, 2, 1)]
Output : [(1, 2, 0)]

Method #1 : Using nested loops
This is one of the ways in which this task can be performed. This is brute force method, in which we loop for each tuple, a matching tuple w.r.t size and sum and perform removal.

 `# Python3 code to demonstrate working of  ` `# Remove Equilength and Equisum Tuple Duplicates ` `# Using nested loop ` ` `  `# initializing lists ` `test_list ``=` `[(``4``, ``5``, ``6``), (``3``, ``0``), (``2``, ``1``), (``1``, ``2``, ``3``), (``5``, ``5``, ``5``)] ` ` `  `# printing original list ` `print``(``"The original list is : "` `+` `str``(test_list)) ` ` `  `# Remove Equilength and Equisum Tuple Duplicates ` `# Using nested loop ` `res ``=` `[] ` `for` `sub ``in` `test_list: ` `    ``for` `sub1 ``in` `res: ` `        ``if` `len``(sub) ``=``=` `len``(sub1) ``and` `sum``(sub) ``=``=` `sum``(sub1): ` `            ``break` `    ``else``: ` `        ``res.append(sub) ` ` `  `# printing result  ` `print``(``"Tuples after filteration : "` `+` `str``(res))  `

Output :

```The original list is : [(4, 5, 6), (3, 0), (2, 1), (1, 2, 3), (5, 5, 5)]
Tuples after filteration : [(4, 5, 6), (3, 0), (1, 2, 3)]
```

Method #2 : Using `dict() + values()`
The combination of above functions offer another way to solve this problem. In this, we just harness the property of dictionary of having unique keys and create a tuple key with len and sum of tuples. The duplicates are thus, avoided.

 `# Python3 code to demonstrate working of  ` `# Remove Equilength and Equisum Tuple Duplicates ` `# Using dict() + values() ` ` `  `# initializing lists ` `test_list ``=` `[(``4``, ``5``, ``6``), (``3``, ``0``), (``2``, ``1``), (``1``, ``2``, ``3``), (``5``, ``5``, ``5``)] ` ` `  `# printing original list ` `print``(``"The original list is : "` `+` `str``(test_list)) ` ` `  `# Remove Equilength and Equisum Tuple Duplicates ` `# Using dict() + values() ` `res ``=` `list``({(``len``(sub), ``sum``(sub)): sub ``for` `sub ``in` `test_list}.values()) ` ` `  `# printing result  ` `print``(``"Tuples after filteration : "` `+` `str``(res))  `

Output :

```The original list is : [(4, 5, 6), (3, 0), (2, 1), (1, 2, 3), (5, 5, 5)]
Tuples after filteration : [(4, 5, 6), (3, 0), (1, 2, 3)]
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.