
- 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
Python program to extract rows from Matrix that has distinct data types
When it is required to extract rows from a matrix with different data types, it is iterated over and ‘set’ is used to get the distinct types.
Example
Below is a demonstration of the same
my_list = [[4, 2, 6], ["python", 2, {6: 2}], [3, 1, "fun"], [9, (4, 3)]] print("The list is :") print(my_list) my_result = [] for sub in my_list: type_size = len(list(set([type(ele) for ele in sub]))) if len(sub) == type_size: my_result.append(sub) print("The resultant distinct data type rows are :") print(my_result)
Output
The list is : [[4, 2, 6], ['python', 2, {6: 2}], [3, 1, 'fun'], [9, (4, 3)]] The resultant distinct data type rows are : [['python', 2, {6: 2}], [9, (4, 3)]]
Explanation
A list of different data types is defined and is displayed on the console
An empty list is defined.
The original list is iterated over, and the type of every element is determined.
It is converted into a set type, and then into a list.
Its size is determined, and it is compared with the specific size.
If they match, it is appended to the empty list.
This is displayed as output on the console.
- Related Articles
- Python – Extract rows with Complex data types
- Python Program to Extract Rows of a matrix with Even frequency Elements
- Python – Extract Particular data type rows
- Python Program that prints the rows of a given length from a matrix
- Find distinct elements common to all rows of a matrix in Python
- Python program to extract rows with common difference elements
- Python – Extract String elements from Mixed Matrix
- Python Program that filters out non-empty rows of a matrix
- Program to find number of distinct island shapes from a given matrix in Python
- Python – Extract Paired Rows
- Python Program that extract words starting with Vowel From A list
- Python program to sort matrix based upon sum of rows
- Python program to remove rows with duplicate element in Matrix
- Python program to extract Keywords from a list
- Python - Print rows from the matrix that have same element at a given index

Advertisements