# Python – Remove Similar Rows from Tuple Matrix

Sometimes, while working with Tuple Matrix, we can have a problem in which we get lots of data, which are similar, i.e elements are same in rows, just the ordering of tuples is different, it’s sometimes, desired to get them removed. This kind of problem can have application in domains such as web development and day-day programming. Let’s discuss certain ways in which this task can be performed.

Input : test_list = [[(4, 5), (3, 2)], [(3, 2), (4, 5)], [(3, 2), (4, 5)]]
Output : {((4, 5), (3, 2))}

Input : test_list = [[(4, 6), (3, 2)], [(3, 2), (4, 5)], [(3, 2), (4, 5)]]
Output : {((3, 2), (4, 6)), ((4, 5), (3, 2))}

Method #1 : Using `set() + tuple() + sorted()` + list comprehension
The combination of above functions can be used to solve this problem. In this, we first, perform the sorting, and then convert the rows to set, which automatically removes the duplicates.

 `# Python3 code to demonstrate working of  ` `# Remove Similar Rows from Tuple Matrix ` `# Using set() + tuple() + sorted() + list comprehension ` ` `  `# initializing lists ` `test_list ``=` `[[(``4``, ``5``), (``3``, ``2``)], [(``2``, ``2``), (``4``, ``6``)], [(``3``, ``2``), (``4``, ``5``)]] ` ` `  `# printing original list ` `print``(``"The original list is : "` `+` `str``(test_list)) ` ` `  `# Remove Similar Rows from Tuple Matrix ` `# Using set() + tuple() + sorted() + list comprehension ` `res ``=` `set``([``tuple``(``set``(sub)) ``for` `sub ``in` `test_list]) ` ` `  `# printing result  ` `print``(``"Tuple matrix after removal : "` `+` `str``(res)) `

Output :

```The original list is : [[(4, 5), (3, 2)], [(2, 2), (4, 6)], [(3, 2), (4, 5)]]
Tuple matrix after removal : {((4, 6), (2, 2)), ((4, 5), (3, 2))}
```

Method #2 : Using `set() + frozenset()` + generator expression
The combination of above functions can be used to solve this problem. In this, we perform the task of sorting and tuple conversion using frozenset().

 `# Python3 code to demonstrate working of  ` `# Remove Similar Rows from Tuple Matrix ` `# Using set() + frozenset() + generator expression ` ` `  `# initializing lists ` `test_list ``=` `[[(``4``, ``5``), (``3``, ``2``)], [(``2``, ``2``), (``4``, ``6``)], [(``3``, ``2``), (``4``, ``5``)]] ` ` `  `# printing original list ` `print``(``"The original list is : "` `+` `str``(test_list)) ` ` `  `# Remove Similar Rows from Tuple Matrix ` `# Using set() + frozenset() + generator expression ` `res ``=` `set``([``frozenset``(sub) ``for` `sub ``in` `test_list]) ` ` `  `# printing result  ` `print``(``"Tuple matrix after removal : "` `+` `str``(res)) `

Output :

```The original list is : [[(4, 5), (3, 2)], [(2, 2), (4, 6)], [(3, 2), (4, 5)]]
Tuple matrix after removal : {frozenset({(4, 5), (3, 2)}), frozenset({(4, 6), (2, 2)})}
```

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.