
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Remove similar element rows in tuple Matrix in Python
When it is required to remove similar element rows in a tuple matrix, the list comprehension and the 'all' method can be used.
The list comprehension is a shorthand to iterate through the list and perform operations on it.
The 'all' method checks to see if all the values inside an iterable are True values. If yes, it returns True, else returns False.
Below is a demonstration of the same −
Example
my_tuple_1 = ((11, 14, 0), (78, 33, 11), (10, 78, 0), (78,78,78)) print("The tuple of tuples is : ") print(my_tuple_1) my_result = tuple(ele for ele in my_tuple_1 if not all(sub == ele[0] for sub in ele)) print("The tuple after removing like-element rows is: ") print(my_result)
Output
The tuple of tuples is : ((11, 14, 0), (78, 33, 11), (10, 78, 0), (78, 78, 78)) The tuple after removing like-element rows is: ((11, 14, 0), (78, 33, 11), (10, 78, 0))
Explanation
- A nested tuple is defined and is displayed on the console.
- The tuple is iterated over, and the 'all' method is called on every element of the nested tuple.
- It is then converted into a tuple.
- This is assigned to a value.
- It is displayed on the console.
- Related Articles
- Python – Remove Rows for similar Kth column element
- Python program to remove rows with duplicate element in Matrix
- Python – Check Similar elements in Matrix rows
- Record Similar tuple occurrences in Python
- Remove nested records from tuple in Python
- Remove Element in Python
- How to remove rows that contain NAs in R matrix?
- Maximum element in tuple list in Python
- Conversion to N*N tuple matrix in Python
- Python - Remove positional rows
- Python – Rows with K string in Matrix
- Python – Test if Rows have Similar frequency
- Get tuple element data types in Python
- Python Program to sort rows of a matrix by custom element count
- Python – Test if all rows contain any common element with other Matrix

Advertisements