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 integer and tuple, different data types, hence included in results.
Input : test_list = [[4, 3, 1], [“gfg”, 3, {4:2}, 4], [3, 1, “jkl”], [9, (2, 3)]]
Output : [[9, (2, 3)]]
Explanation : [4, 3, 1] are all integers hence omitted. [9, (2, 3)] has integer and tuple, different data types, hence included in results.
Method 1 : Using type() + list comprehension
In this, we use type() to check for data types of each element of rows, and if the data type repeats, the row is not included in the result.
Python3
test_list = [[ 4 , 3 , 1 ], [ "gfg" , 3 , { 4 : 2 }], [ 3 , 1 , "jkl" ], [ 9 , ( 2 , 3 )]]
print ( "The original list is : " + str (test_list))
res = []
for sub in test_list:
type_size = len ( list ( set ([ type (ele) for ele in sub])))
if len (sub) = = type_size:
res.append(sub)
print ( "The Distinct data type rows : " + str (res))
|
Output
The original list is : [[4, 3, 1], ['gfg', 3, {4: 2}], [3, 1, 'jkl'], [9, (2, 3)]]
The Distinct data type rows : [['gfg', 3, {4: 2}], [9, (2, 3)]]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 2: Using type()
Python3
test_list = [[ 4 , 3 , 1 ], [ "gfg" , 3 , { 4 : 2 }], [ 3 , 1 , "jkl" ], [ 9 , ( 2 , 3 )]]
print ( "The original list is : " + str (test_list))
res = []
for sub in test_list:
c = 0
for j in range ( 0 , len (sub)):
if ( type (sub[ 0 ]) = = type (sub[j])):
c + = 1
if (c ! = len (sub)):
res.append(sub)
print ( "The Distinct data type rows : " + str (res))
|
Output
The original list is : [[4, 3, 1], ['gfg', 3, {4: 2}], [3, 1, 'jkl'], [9, (2, 3)]]
The Distinct data type rows : [['gfg', 3, {4: 2}], [3, 1, 'jkl'], [9, (2, 3)]]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 4: Using reduce()
Python3
test_list = [[ 4 , 3 , 1 ], [ "gfg" , 3 , { 4 : 2 }],
[ 3 , 1 , "jkl" ], [ 9 , ( 2 , 3 )]]
print ( "The original list is : " + str (test_list))
res = list ( filter ( lambda sub: len ( set ( map ( type , sub))) > 1 , test_list))
print ( "The Distinct data type rows : " + str (res))
|
Output
The original list is : [[4, 3, 1], ['gfg', 3, {4: 2}], [3, 1, 'jkl'], [9, (2, 3)]]
The Distinct data type rows : [['gfg', 3, {4: 2}], [3, 1, 'jkl'], [9, (2, 3)]]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 4: Using collections.defaultdict class from the collections module.
Approach:
- Initialize an empty list called res, which will be used to store the rows that have more than one distinct data type.
- Loop through each row in test_list using a for loop.
- For each row, initialize an empty set called seen_types, which will be used to keep track of the data types seen so far.
- Loop through each item in the row using another for loop.
- For each item, use the type() function to get its data type and store it in a variable called item_type.
- Check if item_type is already in seen_types using an if statement.
- If item_type is not in seen_types, add it to the set using seen_types.add(item_type).
- After processing all the items in the row, check if seen_types has more than one distinct data type using len(seen_types) > 1.
- If seen_types has more than one distinct data type, append the row to res using res.append(row).
- After processing all the rows, print the result using print(“The Distinct data type rows : ” + str(res)).
Example:
Python3
test_list = [[ 4 , 3 , 1 ], [ "gfg" , 3 , { 4 : 2 }], [ 3 , 1 , "jkl" ], [ 9 , ( 2 , 3 )]]
print ( "The original list is : " + str (test_list))
res = []
for row in test_list:
seen_types = set ()
for item in row:
item_type = type (item)
if item_type not in seen_types:
seen_types.add(item_type)
if len (seen_types) > 1 :
res.append(row)
print ( "The Distinct data type rows : " + str (res))
|
Output
The original list is : [[4, 3, 1], ['gfg', 3, {4: 2}], [3, 1, 'jkl'], [9, (2, 3)]]
The Distinct data type rows : [['gfg', 3, {4: 2}], [3, 1, 'jkl'], [9, (2, 3)]]
Time complexity: O(n * m), where n is the number of rows in the input list and m is the maximum number of elements in a row.
Auxiliary Space: O(m), because the seen_types set can store at most one instance of each data type in a row.
Method 5: Using set and isinstance()
Use the set() function to find unique data types in each row, and the isinstance() function to check if a value is an instance of a particular data type.
Step-by-step approach:
- Initialize an empty list res.
- Iterate over each sublist sub in test_list.
- Initialize an empty set types to store the unique data types in sub.
- Iterate over each element elem in sub.
- Check if type(elem) is already in types. If not, add it to types.
- If the length of types is greater than 1, it means there are multiple data types in sub. In that case, append sub to res.
- Return res.
Python3
test_list = [[ 4 , 3 , 1 ], [ "gfg" , 3 , { 4 : 2 }],
[ 3 , 1 , "jkl" ], [ 9 , ( 2 , 3 )]]
print ( "The original list is : " + str (test_list))
res = []
for sub in test_list:
types = set ()
for elem in sub:
types.add( type (elem))
if len (types) > 1 :
res.append(sub)
print ( "The Distinct data type rows : " + str (res))
|
Output
The original list is : [[4, 3, 1], ['gfg', 3, {4: 2}], [3, 1, 'jkl'], [9, (2, 3)]]
The Distinct data type rows : [['gfg', 3, {4: 2}], [3, 1, 'jkl'], [9, (2, 3)]]
Time complexity: O(nm), where n is the number of sublists in test_list and m is the maximum length of any sublist.
Auxiliary space: O(k), where k is the maximum number of unique data types in any sublist of test_list.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
15 May, 2023
Like Article
Save Article