
- 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 rows with Complex data types
When it is required to extract rows with complex data types, the ‘isinstance’ method and list comprehension are used.
Example
Below is a demonstration of the same
my_list = [[13, 1,35], [23, [44, 54], 85], [66], [75, (81, 2), 29, 7]] my_result = [row for row in my_list if any(isinstance(element, list) or isinstance(element, tuple) or isinstance(element, dict) or isinstance(element, set) for element in row)] print("The list is :") print(my_list) print("The resultant list is :") print(my_result)
Output
The list is : [[13, 1, 35], [23, [44, 54], 85], [66], [75, (81, 2), 29, 7]] The resultant list is : [[23, [44, 54], 85], [75, (81, 2), 29, 7]]
Explanation
A list of list is defined and is displayed on the console.
A list comprehension is used to iterate over the list and see if the element belongs to the ‘list’ type using the ‘isinstance’ method.
This is assigned to a variable.
This is displayed as the output on the console.
- Related Articles
- Python program to extract rows from Matrix that has distinct data types
- Python – Extract Particular data type rows
- Python – Extract rows with Even length strings
- Python – Extract Paired Rows
- What are Complex Data types in JavaScript?
- Python program to extract rows with common difference elements
- Python Program to Extract Rows of a matrix with Even frequency Elements
- How to extract data from a string with Python Regular Expressions?
- Python Data Types for Data Science
- How to extract unique combination of rows in an R data frame?
- How to Extract Wikipedia Data in Python?
- Standard Data Types in Python
- Which data types are immutable in Python?
- Get tuple element data types in Python
- Python – Remove rows with Numbers

Advertisements