Python program to remove rows with duplicate element in Matrix
Last Updated :
10 Apr, 2023
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 unique row.
Method 1 : Using list comprehension + set() + len()
In this, we extract only the rows which remain in the same length after converting it into a set.
Python3
# initializing list
test_list = [[4, 3, 2], [7, 6, 7], [2, 4, 5], [8, 9, 9]]
# printing original list
print("The original list is : " + str(test_list))
# set() removing all elements
# list comprehension used to filter
res = [sub for sub in test_list if len(set(sub)) == len(sub)]
# printing result
print("Rows after removal : " + str(res))
OutputThe original list is : [[4, 3, 2], [7, 6, 7], [2, 4, 5], [8, 9, 9]]
Rows after removal : [[4, 3, 2], [2, 4, 5]]
Time Complexity: O(n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(1) additional space is not needed.
Method #2 : Using filter() + lambda + set() + len()
In this, we perform the task of filtering using filter() + lambda function, and set and len() are used to check.
Python3
# initializing list
test_list = [[4, 3, 2], [7, 6, 7], [2, 4, 5], [8, 9, 9]]
# printing original list
print("The original list is : " + str(test_list))
# set() removing all elements
# filter() used to filter
res = list(filter(lambda ele: len(set(ele)) == len(ele), test_list))
# printing result
print("Rows after removal : " + str(res))
OutputThe original list is : [[4, 3, 2], [7, 6, 7], [2, 4, 5], [8, 9, 9]]
Rows after removal : [[4, 3, 2], [2, 4, 5]]
Method #3: Using Counter() function
Python3
from collections import Counter
# initializing list
test_list = [[4, 3, 2], [7, 6, 7], [2, 4, 5], [8, 9, 9]]
# printing original list
print("The original list is : " + str(test_list))
for i in test_list.copy():
freq = Counter(i)
if(len(freq)!=len(i)):
test_list.remove(i)
# printing result
print("Rows after removal : " + str(test_list))
OutputThe original list is : [[4, 3, 2], [7, 6, 7], [2, 4, 5], [8, 9, 9]]
Rows after removal : [[4, 3, 2], [2, 4, 5]]
Time Complexity:O(N*N)
Auxiliary Space :O(N)
Method #4: Using for loop and an if condition
Python3
# initializing list
test_list = [[4, 3, 2], [7, 6, 7], [2, 4, 5], [8, 9, 9]]
# printing original list
print("The original list is : " + str(test_list))
res = []
for sub in test_list:
if len(set(sub)) == len(sub):
res.append(sub)
# printing result
print("Rows after removal : " + str(res))
#This code is contributed by Vinay Pinjala.
OutputThe original list is : [[4, 3, 2], [7, 6, 7], [2, 4, 5], [8, 9, 9]]
Rows after removal : [[4, 3, 2], [2, 4, 5]]
Time Complexity:O(N*NlogN)
Auxiliary Space :O(N)
Method#5: Using Recursive method.
Algorithm:
- Check if the list is empty. If it is, return an empty list.
- Check if the first sub-list in the list has duplicate elements. If it doesn't, include it in the result list and call the function recursively with the remaining sub-lists.
- If it does have duplicate elements, skip it and call the function recursively with the remaining sub-lists.
- Return the result list.
Python3
def remove_duplicate_rows(test_list):
if not test_list:
return []
# check if the first sub-list has duplicate elements
if len(set(test_list[0])) == len(test_list[0]):
# if not, include it in the result and call the function recursively with the remaining sub-lists
return [test_list[0]] + remove_duplicate_rows(test_list[1:])
else:
# if it has duplicate elements, skip it and call the function recursively with the remaining sub-lists
return remove_duplicate_rows(test_list[1:])
# initializing list
test_list = [[4, 3, 2], [7, 6, 7], [2, 4, 5], [8, 9, 9]]
# printing original list
print("The original list is : " + str(test_list))
res = remove_duplicate_rows(test_list)
# printing result
print("Rows after removal : " + str(res))
#This code is contributed by tvsk.
OutputThe original list is : [[4, 3, 2], [7, 6, 7], [2, 4, 5], [8, 9, 9]]
Rows after removal : [[4, 3, 2], [2, 4, 5]]
Time complexity: O(n^2), where n is the total number of elements in the list. This is because we need to compare each sub-list with every other sub-list to determine if it has duplicate elements.
Auxiliary Space: O(n), where n is the total number of elements in the list. This is because we are creating a new list to store the non-duplicate sub-lists.
Method 6: Using the built-in all() function:
Step-by-step approach:
- Initialize an empty list to store the rows that don't have duplicate elements.
- Use a list comprehension to iterate over each row of the input list.
- For each row, create a set and check if the length of the set is equal to the length of the row using the all() function.
- If all() elements in the row are unique, append the row to the list from step 1.
- Print the resulting list.
Below is the implementation of the above approach:
Python3
# initializing list
test_list = [[4, 3, 2], [7, 6, 7], [2, 4, 5], [8, 9, 9]]
# printing original list
print("The original list is : " + str(test_list))
# using all() function to check if all elements are unique
res = [sub for sub in test_list if all(sub.count(elem) == 1 for elem in sub)]
# printing result
print("Rows after removal : " + str(res))
OutputThe original list is : [[4, 3, 2], [7, 6, 7], [2, 4, 5], [8, 9, 9]]
Rows after removal : [[4, 3, 2], [2, 4, 5]]
Time complexity: O(n*m), where n is the number of rows and m is the maximum length of a row.
Auxiliary space: O(k), where k is the number of rows that don't have duplicate elements.
Similar Reads
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 program to remove duplicate elements index from other list
Given two lists, the task is to write a Python program to remove all the index elements from 2nd list which are duplicate element indices from 1st list. Examples: Input : test_list1 = [3, 5, 6, 5, 3, 7, 8, 6], test_list2 = [1, 7, 6, 3, 7, 9, 10, 11] Output : [1, 7, 6, 9, 10] Explanation : 3, 7 and 1
7 min read
Python Program to Remove First Diagonal Elements from a Square Matrix
Given a square matrix of N*N dimension, the task is to write a Python program to remove the first diagonal. Examples: Input : test_list = [[5, 3, 3, 2, 1], [5, 6, 7, 8, 2], [9, 3, 4, 6, 7], [0, 1, 2, 3, 5], [2, 5, 4, 3, 5]] Output : [[3, 3, 2, 1], [5, 7, 8, 2], [9, 3, 6, 7], [0, 1, 2, 5], [2, 5, 4,
6 min read
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 duplicates in Matrix
While working with Python Matrix, we can face a problem in which we need to perform the removal of duplicates from Matrix. This problem can occur in Machine Learning domain because of extensive usage of matrices. Let's discuss certain way in which this task can be performed. Method : Using loop This
2 min read
Python program to extract rows with common difference elements
Given a Matrix, extract rows with AP sequence. Input : test_list = [[4, 7, 10], [8, 10, 12], [10, 11, 13], [6, 8, 10]] Output : [[4, 7, 10], [8, 10, 12], [6, 8, 10]] Explanation : 3, 4, and 2 are common difference in AP. Input : test_list = [[4, 7, 10], [8, 10, 13], [10, 11, 13], [6, 8, 10]] Output
3 min read
Python Program to Remove duplicate tuples irrespective of order
Given a list of binary tuples, the task is to write a Python program to remove all tuples that are duplicates irrespective of order, i.e delete if contains similar elements, irrespective of order. Input : test_list = [(4, 6), (1, 2), (9, 2), (2, 1), (5, 7), (6, 4), (9, 2)]Output : [(1, 2), (5, 7), (
6 min read
Python Program to Extract Rows of a matrix with Even frequency Elements
Given a Matrix, the task is to write a Python program to extract all the rows which have even frequencies of elements. Examples: Input: [[4, 5, 5, 2], [4, 4, 4, 4, 2, 2], [6, 5, 6, 5], [1, 2, 3, 4]] Output: [[4, 4, 4, 4, 2, 2], [6, 5, 6, 5]]Explanation: frequency of 4-> 4 which is even frequency
5 min read
Python Program for Last duplicate element in a sorted array
We have a sorted array with duplicate elements and we have to find the index of last duplicate element and print index of it and also print the duplicate element. If no such element found print a message. Examples: Input : arr[] = {1, 5, 5, 6, 6, 7} Output : Last index: 4 Last duplicate item: 6 Inpu
2 min read
Python | Row with Minimum element in Matrix
We can have an application for finding the lists with the minimum value and print it. This seems quite an easy task and may also be easy to code, but sometimes we need to print the entire row containing it and having shorthands to perform the same are always helpful as this kind of problem can come
5 min read