Skip to content
Related Articles
Open in App
Not now

Related Articles

Python – Extract Particular data type rows

Improve Article
Save Article
  • Last Updated : 22 Jul, 2022
Improve Article
Save Article

Given A Matrix, extract all the rows which have all the elements with particular data type.

Input : test_list = [[4, 5, “Hello”], [2, 6, 7], [“g”, “f”, “g”], [9, 10, 11]], data_type = int 
Output : [[2, 6, 7], [9, 10, 11]] 
Explanation : All lists with integer are extracted.

Input : test_list = [[4, 5, “Hello”], [2, 6, 7], [“g”, “f”, “g”], [9, 10, 11]], data_type = str 
Output : [[“g”, “f”, “g”]] 
Explanation : All lists with strings are extracted. 

Method #1 : Using isinstance() + all() + list comprehension

In this, we check for data type using isinstance(), all() checks for all the elements being of particular data type. 

Python3




# Python3 code to demonstrate working of
# Extract Particular data type rows
# Using isinstance() + all() + list comprehension
 
# initializing list
test_list = [[4, 5, "Hello"], [2, 6, 7], ["g", "f", "g"], [9, 10, 11]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing data type
data_type = int
 
# checking data type using isinstance
res = [row for row in test_list if all(
    isinstance(ele, data_type) for ele in row)]
 
# printing result
print("Filtered Rows : " + str(res))

Output:

The original list is : [[4, 5, ‘Hello’], [2, 6, 7], [‘g’, ‘f’, ‘g’], [9, 10, 11]] Filtered Rows : [[2, 6, 7], [9, 10, 11]]

Method #2 : Using filter() + lambda + isinstance()

In this, we perform task of filtering using filter() and lambda, isinstance(), is used to perform task of type check as in case of above method.

Python3




# Python3 code to demonstrate working of
# Extract Particular data type rows
# Using filter() + lambda + isinstance()
 
# initializing list
test_list = [[4, 5, "Hello"], [2, 6, 7], ["g", "f", "g"], [9, 10, 11]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing data type
data_type = int
 
# checking data type using isinstance
# filter() used to get filter
res = list(filter(lambda row: all(isinstance(ele, data_type)
                                  for ele in row), test_list))
 
# printing result
print("Filtered Rows : " + str(res))

Output:

The original list is : [[4, 5, ‘Hello’], [2, 6, 7], [‘g’, ‘f’, ‘g’], [9, 10, 11]] Filtered Rows : [[2, 6, 7], [9, 10, 11]]

Method #3: Using type().type() returns the data type of variable.

Python3




# Python3 code to demonstrate working of
# Extract Particular data type rows
 
# initializing list
test_list = [[4, 5, "Hello"], [2, 6, 7], ["g", "f", "g"], [9, 10, 11]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing data type
data_type = int
res=[]
# checking data type
for i in test_list:
    c=0
    for j in i:
        if(type(j) is data_type):
            c+=1
    if(c==len(i)):
        res.append(i)
 
# printing result
print("Filtered Rows : " + str(res))

Output

The original list is : [[4, 5, 'Hello'], [2, 6, 7], ['g', 'f', 'g'], [9, 10, 11]]
Filtered Rows : [[2, 6, 7], [9, 10, 11]]

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!