Python | Remove similar element rows in tuple Matrix
Last Updated :
27 Apr, 2023
Sometimes, while working with data, we can have a problem in which we need to remove elements from the 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
test_tup = (( 1 , 3 , 5 ), ( 2 , 2 , 2 ),
( 9 , 10 , 10 ), ( 4 , 4 , 4 ))
print ( "The original tuple : " + str (test_tup))
res = tuple (ele for ele in test_tup if not all (sub = = ele[ 0 ] for sub in ele))
print ( "The tuple after removal of like-element rows : " + str (res))
|
Output :
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))
Time Complexity: O(n*n), where n is the length of the list test_tup
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
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
test_tup = (( 1 , 3 , 5 ), ( 2 , 2 , 2 ),
( 9 , 10 , 10 ), ( 4 , 4 , 4 ))
print ( "The original tuple : " + str (test_tup))
res = tuple (ele for ele in test_tup if len ( set (ele)) > 1 )
print ( "The tuple after removal of like-element rows : " + str (res))
|
Output :
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))
Time complexity: O(nm), where n is the number of rows and m is the number of columns.
Auxiliary space: O(n), where n is the number of rows.
Method #3 : Using count() and len() methods
Python3
test_tup = (( 1 , 3 , 5 ), ( 2 , 2 , 2 ),
( 9 , 10 , 10 ), ( 4 , 4 , 4 ))
print ( "The original tuple : " + str (test_tup))
res = []
for i in test_tup:
if (i.count(i[ 0 ])! = len (i)):
res.append(i)
res = tuple (res)
print ( "The tuple after removal of like-element rows : " + str (res))
|
Output
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))
The time complexity of this program is O(n * m), where n is the number of rows and m is the number of columns in the tuple.
The auxiliary space complexity of this program is also O(n * m). The program initializes an empty list res to store the rows that don’t have all like elements.
Method #4 : Using Counter() function
Python3
from collections import Counter
test_tup = (( 1 , 3 , 5 ), ( 2 , 2 , 2 ),
( 9 , 10 , 10 ), ( 4 , 4 , 4 ))
print ( "The original tuple : " + str (test_tup))
res = []
for i in test_tup:
freq = Counter(i)
if ( len (freq) ! = 1 ):
res.append(i)
res = tuple (res)
print ( "The tuple after removal of like-element rows : " + str (res))
|
Output
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))
Time Complexity:O(n*n)
Auxiliary Space: O(n)
Method 5: using operator.countOf() method
Python3
import operator as op
test_tup = (( 1 , 3 , 5 ), ( 2 , 2 , 2 ),
( 9 , 10 , 10 ), ( 4 , 4 , 4 ))
print ( "The original tuple : " + str (test_tup))
res = []
for i in test_tup:
if (op.countOf(i, i[ 0 ]) ! = len (i)):
res.append(i)
res = tuple (res)
print ( "The tuple after removal of like-element rows : " + str (res))
|
Output
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))
Time Complexity: O(N*M)
Auxiliary Space : O(N*M)
Method#6: Using Recursive method.
The algorithm for the recursive method to remove similar element rows in a tuple matrix is as follows:
- Define a function remove_similar_rows that takes in one argument: test_tup.
- Check if test_tup is empty.
- If test_tup is empty, return an empty tuple.
- Otherwise, check if all elements in the first row of test_tup are equal to the first element of the first row.
- If all elements in the first row of test_tup are equal to the first element of the first row, call remove_similar_rows recursively with the remaining rows in test_tup.
- Otherwise, return a new tuple consisting of the first row in test_tup followed by the result of calling remove_similar_rows recursively with the remaining rows in test_tup.
Python3
def remove_similar_rows(test_tup):
if not test_tup:
return ()
elif all (sub = = test_tup[ 0 ][ 0 ] for sub in test_tup[ 0 ]):
return remove_similar_rows(test_tup[ 1 :])
else :
return (test_tup[ 0 ],) + remove_similar_rows(test_tup[ 1 :])
test_tup = (( 1 , 3 , 5 ), ( 2 , 2 , 2 ), ( 9 , 10 , 10 ), ( 4 , 4 , 4 ))
print ( "The original tuple : " + str (test_tup))
res = remove_similar_rows(test_tup)
print ( "The tuple after removal of like-element rows : " + str (res))
|
Output
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))
The time complexity of this algorithm is O(n * m), where n is the number of rows and m is the number of columns in test_tup. This is because we need to iterate over all rows and columns in test_tup to remove similar element rows.
The auxiliary space of this algorithm is O(n), where n is the number of rows in test_tup. This is because we need to store n recursive calls on the call stack and create a new tuple to store the result.
Similar Reads
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 de
9 min read
Python - Check Similar elements in Matrix rows
Given a Matrix and list, the task is to write a Python program to check if all the matrix elements of the row are similar to the ith index of the List. Input : test_list = [[1, 1, 1], [4, 4], [3, 3, 3], [5, 5, 5, 5]] Output : True Explanation : All rows have same elements.Input : test_list = [[1, 1,
8 min read
Python - Remove Rows for similar Kth column element
Given a Matrix, remove row if similar element has occurred in row above in Kth column. Input : test_list = [[3, 4, 5], [2, 3, 5], [10, 4, 3], [7, 8, 9], [9, 3, 6]], K = 2 Output : [[3, 4, 5], [10, 4, 3], [7, 8, 9], [9, 3, 6]] Explanation : In [2, 3, 5], we already has list [3, 4, 5] having 5 at K, i
7 min read
Python - Similar index elements Matrix
Sometimes, while working with data, we can have a problem in which we need to perform the construction of matrix from lists element vertically than horizontally. This kind of application can come in Data Science domains in which we need to construct Matrix from several lists. Lets discuss certain wa
7 min read
Python program to remove rows with duplicate element in Matrix
Given Matrix, remove all rows which have duplicate elements in them. Input : test_list = [[4, 3, 2], [7, 6, 7], [2, 4, 4], [8, 9, 9]] Output : [[4, 3, 2]] Explanation : [4, 3, 2] is the only unique row. Input : test_list = [[4, 3, 3, 2], [7, 6, 7], [2, 4, 4], [8, 9, 9]] Output : [] Explanation : No
5 min read
Python - Group similar elements into Matrix
Sometimes, while working with Python Matrix, we can have a problem in which we need to perform grouping of all the elements with are the same. This kind of problem can have applications in data domains. Let's discuss certain ways in which this task can be performed. Input : test_list = [1, 3, 4, 4,
8 min read
Python | Remove last element from each row in Matrix
Sometimes, while working with Matrix data, we can have a stray element attached at rear end of each row of matrix. This can be undesired at times and wished to be removed. Let's discuss certain ways in which this task can be performed. Method #1: Using loop + del + list slicing The combination of th
6 min read
Python program to remove last element from Tuple
Given a tuple, the task is to write a Python program to delete the last element in the tuple in Python. Example: Input: ("geeks", "for", "geeks") Output:("geeks", "for") Explanation: Here we are deleting the last element of the tuple and finally modifying the original one. Note: Tuples are immutable
3 min read
Python - Join Tuples if similar initial element
Sometimes, while working with Python tuples, we can have a problem in which we need to perform concatenation of records from the similarity of initial element. This problem can have applications in data domains such as Data Science. Let's discuss certain ways in which this task can be performed. Inp
8 min read
Search Elements in a Matrix - Python
The task of searching for elements in a matrix in Python involves checking if a specific value exists within a 2D list or array. The goal is to efficiently determine whether the desired element is present in any row or column of the matrix. For example, given a matrix a = [[4, 5, 6], [10, 2, 13], [1
3 min read