- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python – Extract String elements from Mixed Matrix
When it is required to extract string elements from mixed matrix, a list comprehension and the ‘isinstance’ method is used.
Example
Below is a demonstration of the same −
my_list = [[35, 66, 31], ["python", 13, "is"], [15, "fun", 14]] print("The list is :") print(my_list) my_result = [element for index in my_list for element in index if isinstance(element, str)] print("The result is :") print(my_result)
Output
The list is : [[35, 66, 31], ['python', 13, 'is'], [15, 'fun', 14]] The result is : ['python', 'is', 'fun']
Explanation
A list of list is defined and displayed on the console.
A list comprehension is used to iterate over the elements in the list.
The ‘isinstance’ method is used to check if the elements in the list of list belong to a specific type.
If yes, it is converted to a list and is assigned to a variable.
This list is displayed as the output on the console.
- Related Articles
- Python – Extract range of Consecutive similar elements ranges from string list
- Python – Extract Percentages from String
- Python – Extract elements from Ranges in List
- Python Program to Extract Rows of a matrix with Even frequency Elements
- Extract decimal numbers from a string in Python
- Extract only characters from given string in Python
- How to extract numbers from a string using Python?
- How to extract numbers from a string in Python?
- How to extract date from a string in Python?
- Python program to extract rows from Matrix that has distinct data types
- Python Regex to extract maximum numeric value from a string
- Python – How to Extract all the digits from a String
- Python Program to Extract Elements from a List in a Set
- How to extract data from a string with Python Regular Expressions?
- How to extract a substring from inside a string in Python?

Advertisements