Python | Remove similar element rows in tuple Matrix
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))
Recommended Posts:
- Python | Remove particular element from tuple list
- Python | Remove duplicates based on Kth element tuple list
- Python | Record Similar tuple occurrences
- Python | Remove last element from each row in Matrix
- Python | Sort tuple list by Nth element of tuple
- Python | Replace tuple according to Nth tuple element
- Python | Remove Consecutive tuple according to key
- Python | Remove tuple from list of tuples if not containing any character
- Python | Print unique rows in a given boolean matrix using Set with tuples
- Python Counter| Find duplicate rows in a binary matrix
- Python | Conversion to N*N tuple matrix
- Python | Adding N to Kth tuple element
- Python | Counting Nth tuple element
- Python | Check if element is present in tuple
- Python | Get tuple element data types
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.