Sometimes, while working with data, we can have a problem in which we need to remove elements from tuple matrix on a condition that if all elements in row of tuple matrix is same. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using list comprehension + all()
This task can be performed using combination of above functions. In this, we traverse all rows using list comprehension and remove all elements that match the initial element in row’s column with help of all().
# Python3 code to demonstrate working of # Remove similar element rows in tuple Matrix # using list comprehension + all() # initialize tuple test_tup = ((1, 3, 5), (2, 2, 2), (9, 10, 10), (4, 4, 4)) # printing original tuple print("The original tuple : " + str(test_tup)) # Remove similar element rows in tuple Matrix # using list comprehension + all() res = tuple(ele for ele in test_tup if not all(sub == ele[0] for sub in ele)) # printing result print("The tuple after removal of like-element rows : " + str(res)) |
The original tuple : ((1, 3, 5), (2, 2, 2), (9, 10, 10), (4, 4, 4)) The tuple after removal of like-element rows : ((1, 3, 5), (9, 10, 10))
Method #2 : Using set() + generator expression
This task can also be performed using the given functionalities. In this, we just check for length of reduced row using set() to be greater than 1. If yes, we know that it is the target row to be removed.
# Python3 code to demonstrate working of # Remove similar element rows in tuple Matrix # using set() + generator expression # initialize tuple test_tup = ((1, 3, 5), (2, 2, 2), (9, 10, 10), (4, 4, 4)) # printing original tuple print("The original tuple : " + str(test_tup)) # Remove similar element rows in tuple Matrix # using set() + generator expression res = tuple(ele for ele in test_tup if len(set(ele)) > 1) # printing result print("The tuple after removal of like-element rows : " + str(res)) |
The original tuple : ((1, 3, 5), (2, 2, 2), (9, 10, 10), (4, 4, 4)) The tuple after removal of like-element rows : ((1, 3, 5), (9, 10, 10))
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python - Remove Similar Rows from Tuple Matrix
- Python - Remove Rows for similar Kth column element
- Python - Convert Tuple Matrix to Tuple List
- Python program to remove rows with duplicate element in Matrix
- Python | Sort tuple list by Nth element of tuple
- Python | Replace tuple according to Nth tuple element
- Python | Record Similar tuple occurrences
- Python - Similar index pairs in Tuple lists
- Python - Test Similar Data Type in Tuple
- Python - Test if Rows have Similar frequency
- Python - Raise elements of tuple as power to another tuple
- Python - Convert Tuple String to Integer Tuple
- Python - Convert Tuple to Tuple Pair
- Python - Flatten tuple of List to tuple
- Python program to convert Set into Tuple and Tuple into Set
- Python | Modify Equal Tuple Rows
- Python - Modify Equal Tuple Rows
- Python | Remove particular element from tuple list
- Python | Remove duplicates based on Kth element tuple list
- Python - Remove given character from first element of Tuple
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.

