
- 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 – Extract Particular data type rows
When it is required to extract particular data type rows, the list comprehension, the ‘isinstance’ method, and the ‘all’ operator are used.
Below is a demonstration of the same −
Example
my_list = [[14,35, "Will"], [12, 26, 17], ["p", "y", "t"], [29, 40, 21]] print("The list is :") print(my_list) my_data_type = int my_result = [row for row in my_list if all(isinstance(element, my_data_type) for element in row)] print("The result is :") print(my_result)
Output
The list is : [[14, 35, 'Will'], [12, 26, 17], ['p', 'y', 't'], [29, 40, 21]] The result is : [[12, 26, 17], [29, 40, 21]]
Explanation
A list of list is defined and is displayed on the console.
The data type is defined.
A list iteration is used to iterate over the list.
The ‘all’ operator and the ‘isinstance’ method is sued to check if the elements in the list belong to a specific data type.
If yes, it is added to a list, and assigned to a variable.
This is displayed as the output on the console.
- Related Articles
- Python – Extract rows with Complex data types
- Python – Extract Paired Rows
- Python program to extract rows from Matrix that has distinct data types
- Python – Extract rows with Even length strings
- Extract a particular level from factor column in an R data frame.
- Python program to extract rows with common difference elements
- How to extract unique combination of rows in an R data frame?
- How to Extract Wikipedia Data in Python?
- How to extract a particular value based on index from an R data frame column?
- Number Data Type in Python
- String Data Type in Python
- List Data Type in Python
- Tuple Data Type in Python
- Dictionary Data Type in Python
- Data Type Conversion in Python

Advertisements