Python - Extract Particular data type rows
Last Updated :
08 Mar, 2023
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]]
Time Complexity: O(n*n)
Auxiliary Space: O(1)
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]]
Time Complexity: O(n), where n is the length of the input list. This is because we’re using the built-in filter() + lambda + isinstance() which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(n), where n is the length of the input list as we’re using additional space other than the input list itself.
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))
OutputThe original list is : [[4, 5, 'Hello'], [2, 6, 7], ['g', 'f', 'g'], [9, 10, 11]]
Filtered Rows : [[2, 6, 7], [9, 10, 11]]
Similar Reads
Python - Extract rows with Complex data types Given Matrix, extract rows with complex data types. Examples: Input : test_list = [[4, 2, 5], [1, [3, 4], 9], [5], [7, (2, 3), 3, 9]] Output : [[1, [3, 4], 9], [7, (2, 3), 3, 9]] Explanation : Rows have lists and tuples respectively. Input : test_list = [[4, 2, [5]], [1, [3, 4], 9], [5], [7, (2, 3),
5 min read
Python program to extract rows from Matrix that has distinct data types Given a Matrix, the task is to write a Python program to extract rows with no repeated data types. Examples: Input : test_list = [[4, 3, 1], ["gfg", 3, {4:2}], [3, 1, "jkl"], [9, (2, 3)]]Output : [['gfg', 3, {4: 2}], [9, (2, 3)]]Explanation : [4, 3, 1] are all integers hence omitted. [9, (2, 3)] has
6 min read
Reading .Dat File in Python Python, with its vast ecosystem of libraries and modules, provides a flexible and efficient environment for handling various file formats, including generic .dat files. In this article, we will different approaches to reading and process .dat files in Python. your_file.dat 1.0 2.0 3.04.0 5.0 6.07.0
2 min read
Get the first 3 rows of a given DataFrame Let us first create a dataframe and then we will try to get first 3 rows of this dataframe using several methods. Code: Creating a Dataframe. Python3 # import pandas library import pandas as pd # dictionary record = { "Name": ["Tom", "Jack", "Lucy", "Bob", "Jerry", "Alice", "Thomas", "Barbie"], "Mar
3 min read
Get the first 3 rows of a given DataFrame Let us first create a dataframe and then we will try to get first 3 rows of this dataframe using several methods. Code: Creating a Dataframe. Python3 # import pandas library import pandas as pd # dictionary record = { "Name": ["Tom", "Jack", "Lucy", "Bob", "Jerry", "Alice", "Thomas", "Barbie"], "Mar
3 min read
Python program to read CSV without CSV module CSV (Comma Separated Values) is a simple file format used to store tabular data, such as a spreadsheet or database. CSV file stores tabular data (numbers and text) in plain text. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the c
3 min read