
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Python program to remove rows with duplicate element in Matrix
When it is required to remove rows with duplicate element in a matrix, a list comprehension and the ‘set’ operator is used.
Example
Below is a demonstration of the same −
my_list = [[34, 23, 34], [17, 46, 47], [22, 14, 22], [28, 91, 19]] print("The list is :") print(my_list) my_result = [element for element in my_list if len(set(element)) == len(element)] print("The result is :") print(my_result)
Output
The list is : [[34, 23, 34], [17, 46, 47], [22, 14, 22], [28, 91, 19]] The result is : [[17, 46, 47], [28, 91, 19]]
Explanation
A list of list is defined and is displayed on the console.
A list comprehension is used to iterate over the elements in the list, and the length of the unique element is compared to the length of every element in the list.
If they are equal, it is stored in the list and assigned to a variable.
This is displayed as the output on the console.
- Related Articles
- Remove similar element rows in tuple Matrix in Python
- Write Python program to find duplicate rows in a binary matrix
- Write a program in Python to remove first duplicate rows in a given dataframe
- How to remove rows in a Pandas series with duplicate indices?
- Java program to remove the duplicate element in an array
- Python Program to sort rows of a matrix by custom element count
- Python program to remove row with custom list element
- Program to remove duplicate entries in a list in Python
- Python – Remove rows with Numbers
- Python Program to remove duplicate elements from a dictionary
- Python – Test if all rows contain any common element with other Matrix
- Python – Remove Rows for similar Kth column element
- Python Program to Extract Rows of a matrix with Even frequency Elements
- Find duplicate rows in a binary matrix in C++
- Program to remove duplicate entries in a linked list in Python

Advertisements