Python – Remove Rows for similar Kth column element
Last Updated :
14 Apr, 2023
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.e 2nd pos.
Input : test_list = [[3, 4, 5], [2, 3, 3], [10, 4, 3], [7, 8, 9], [9, 3, 6]], K = 2
Output : [[3, 4, 5], [2, 3, 3], [7, 8, 9], [9, 3, 6]]
Explanation : In [10, 4, 3], we already has list [2, 3, 3] having 3 at K, i.e 2nd pos.
Method 1: Using loopset
In this, we maintain a memoization container which keeps track of elements in Kth column, if row’s Kth column element is present already, that row is omitted from result.
Python3
test_list = [[ 3 , 4 , 5 ], [ 2 , 3 , 5 ], [ 10 , 4 , 3 ], [ 7 , 8 , 9 ], [ 9 , 3 , 6 ]]
print ( "The original list is : " + str (test_list))
K = 1
res = []
memo = []
for sub in test_list:
if not sub[K] in memo:
res.append(sub)
memo.append(sub[K])
print ( "The filtered Matrix : " + str (res))
|
Output
The original list is : [[3, 4, 5], [2, 3, 5], [10, 4, 3], [7, 8, 9], [9, 3, 6]]
The filtered Matrix : [[3, 4, 5], [2, 3, 5], [7, 8, 9]]
Time complexity: O(M^N) as the number of combinations generated is M choose N.
Auxiliary space: O(M^N) as the size of the resultant list is also M choose N.
Method 2: Using list comprehension.
Use a list comprehension to create a new list that only contains the rows where the Kth element is unique. The code initializes the original list, Kth column, and then filters the list using list comprehension, finally printing the filtered matrix.
Step-by-step approach:
- Initialize the original list.
- Initialize the value of K.
- Use list comprehension to create a new list that only contains the rows where the Kth element is unique.
Below is the implementation of the above approach:
Python3
test_list = [[ 3 , 4 , 5 ], [ 2 , 3 , 5 ], [ 10 , 4 , 3 ], [ 7 , 8 , 9 ], [ 9 , 3 , 6 ]]
print ( "The original list is : " + str (test_list))
K = 1
res = [sub for i, sub in enumerate (test_list) if sub[K] not in [sub2[K] for sub2 in test_list[:i]]]
print ( "The filtered Matrix : " + str (res))
|
Output
The original list is : [[3, 4, 5], [2, 3, 5], [10, 4, 3], [7, 8, 9], [9, 3, 6]]
The filtered Matrix : [[3, 4, 5], [2, 3, 5], [7, 8, 9]]
Time complexity: O(n^2) because of the nested loop.
Auxiliary space: O(n^2) because we are storing the sub-lists in the memo list.
Method#3: Using Recursive method.
Algorithm:
1. Initialize an empty list called `res` and an empty list called `memo`.
2. Loop through each sub-list in the `test_list`.
3. Check if the Kth element of the sub-list is present in the `memo` list.
4. If the Kth element is not present in the `memo` list, append the sub-list to `res` and the Kth element to `memo`.
5. Return the filtered list `res`.
Python3
def remove_similar_rows(test_list, K, memo = None ):
if memo is None :
memo = []
if not test_list:
return []
if test_list[ 0 ][K] in memo:
return remove_similar_rows(test_list[ 1 :], K, memo)
return [test_list[ 0 ]] + remove_similar_rows(test_list[ 1 :], K, memo + [test_list[ 0 ][K]])
test_list = [[ 3 , 4 , 5 ], [ 2 , 3 , 5 ], [ 10 , 4 , 3 ], [ 7 , 8 , 9 ], [ 9 , 3 , 6 ]]
print ( "The original list is : " + str (test_list))
K = 1
res = remove_similar_rows(test_list, K)
print ( "The filtered Matrix : " + str (res))
|
Output
The original list is : [[3, 4, 5], [2, 3, 5], [10, 4, 3], [7, 8, 9], [9, 3, 6]]
The filtered Matrix : [[3, 4, 5], [2, 3, 5], [7, 8, 9]]
The time complexity of this code is O(N^2) where N is the number of sub-lists in the `test_list`.
The space complexity of this code is also O(N^2) because we are storing the filtered list `res` and the `memo` list which can each have N elements in the worst case.
Method 4: Using the Set data structure:
- Initialize an empty set unique_elements.
- Initialize an empty list result[].
- Loop through each sub-list in the given list test_list and check if the element at index K of the current sub-list is present in the unique_elements set or not. If it is not present, add it to the unique_elements set and append the current sub-list to the result list.
- Return the result list.
Below is the implementation of the above approach:
Python3
test_list = [[ 3 , 4 , 5 ], [ 2 , 3 , 5 ], [ 10 , 4 , 3 ], [ 7 , 8 , 9 ], [ 9 , 3 , 6 ]]
K = 1
unique_elements = set ()
result = []
for sub in test_list:
if sub[K] not in unique_elements:
unique_elements.add(sub[K])
result.append(sub)
print ( "The filtered Matrix : " + str (result))
|
Output
The filtered Matrix : [[3, 4, 5], [2, 3, 5], [7, 8, 9]]
The time complexity of this approach is O(N), where N is the number of sub-lists in the given list.
The space complexity is also O(N), as we are storing at most N unique elements in the unique_elements set and at most N sub-lists in the result list.
Method 5: Using heapq:
Algorithm:
- Initialize an empty list res to store the filtered rows.
- Initialize an empty set seen to keep track of the values of column K that have already been seen.
- Iterate over each row in the input test_list:
a. If the value of column K for the current row is not in seen, then add the row to the res list using
- heapq.heappush() and add the value of column K to seen.
- Return the filtered list res.
Python3
import heapq
def remove_similar_rows(test_list, K):
res = []
seen = set ()
for row in test_list:
if row[K] not in seen:
heapq.heappush(res, row)
seen.add(row[K])
return res
test_list = [[ 3 , 4 , 5 ], [ 2 , 3 , 5 ], [ 10 , 4 , 3 ], [ 7 , 8 , 9 ], [ 9 , 3 , 6 ]]
print ( "The original list is : " + str (test_list))
K = 1
res = remove_similar_rows(test_list, K)
print ( "The filtered Matrix : " + str (res))
|
Output
The original list is : [[3, 4, 5], [2, 3, 5], [10, 4, 3], [7, 8, 9], [9, 3, 6]]
The filtered Matrix : [[2, 3, 5], [3, 4, 5], [7, 8, 9]]
Time complexity: O(n log n) where n is the number of rows in the input test_list. This is because the heapq.heappush() operation takes log n time and it is performed for each row in the input list.
Space complexity: O(n) where n is the number of rows in the input test_list. This is because the res list and the seen set each store at most n elements.
Similar Reads
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 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 ta
6 min read
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 | Remove Initial K column elements
Sometimes, while working with Matrix data, we can have stray elements that attached at front 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
4 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
Remove common elements from two list in Python
When working with two lists in Python, we may need to remove the common elements between them. A practical example could be clearing out overlapping tasks between two to-do lists. The most efficient way to remove common elements between two lists is by using sets. [GFGTABS] Python a = [1, 2, 3, 4, 5
3 min read
Python | Remove first K elements matching some condition
Removal of elements in list can be performed using many inbuilt functions. Removing all or just a single occurrence removal both functions are present in Python library. This article discusses to remove just the first K occurrences of elements matching particular condition. Method #1: Naive Method W
3 min read
Python program to remove row with custom list element
Given a matrix, the task here is to write a Python program to remove rows that have any element from the custom list and then display the result. Examples: Input : test_list = [[5, 3, 1], [7, 8, 9], [1, 10, 22], [12, 18, 21]], check_list = [3, 10, 19, 29, 20, 15] Output : [[7, 8, 9], [12, 18, 21]] E
6 min read
Python - Custom Rows Removal depending on Kth Column
Sometimes, while working with Python Matrix, we can have a problem in which we need to remove elements from Matrix depending on its Kth Column element present in the argument list. This can have application in many domains. Let us discuss certain ways in which this task can be performed. Method #1:
7 min read
Python - Remove Columns of Duplicate Elements
Given a Matrix, write a Python program to remove whole column if duplicate occurs in any row. Examples: Input : test_list = [[4, 3, 5, 2, 3], [6, 4, 2, 1, 1], [4, 3, 9, 3, 9], [5, 4, 3, 2, 1]] Output : [[4, 3, 5], [6, 4, 2], [4, 3, 9], [5, 4, 3]] Explanation : 1 has duplicate as next element hence 5
3 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