Friday, November 15, 2024
Google search engine
HomeLanguagesPython – Test if all elements in list are of same type

Python – Test if all elements in list are of same type

Sometimes, while working with Python, we can have a problem in which we need to test if all the elements of argument are of same type or not. This can have application in many domains such as data Science and day-day programming. Lets discuss certain ways in which this task can be performed. 

Method #1: Using loop + isinstance() The combination of above functionalities can be used to perform this task. In this, we test for type using isinstance() and check for all elements if they match same type as of 1st element. 

Python3




# Python3 code to demonstrate
# Test if all elements in list are of same type
# using loop + isinstance()
 
# Initializing lists
test_list = [5, 6, 2, 5, 7, 9]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Test if all elements in list are of same type
# using loop + isinstance()
res = True
for ele in test_list:
    if not isinstance(ele, type(test_list[0])):
        res = False
        break
 
# printing result
print("Do all elements have same type : " + str(res))


Output

The original list is : [5, 6, 2, 5, 7, 9]
Do all elements have same type : True

Time Complexity: O(n) where n is the number of elements in the list “test_list”. 
Auxiliary Space: O(n) where n is the number of elements in the list “test_list”. 

Method #2: Using all() + isinstance() 

This is yet another way to perform this task. In this, we instead of iterating perform the task in one line using all and instance(). 

Python3




# Python3 code to demonstrate
# Test if all elements in list are of same type
# using all() + isinstance()
 
# Initializing lists
test_list = [5, 6, 2, 5, 7, 9]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Test if all elements in list are of same type
# using all() + isinstance()
res = all(isinstance(sub, type(test_list[0])) for sub in test_list[1:])
 
# printing result
print("Do all elements have same type : " + str(res))


Output

The original list is : [5, 6, 2, 5, 7, 9]
Do all elements have same type : True

Time Complexity: O(n), where n is the length of the list test_list 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list 

Method #3 : Using loop+type() method

Python3




# Python3 code to demonstrate
# Test if all elements in list are of same type
 
# Initializing lists
test_list = [5, 6, 2, 5, 7, 9]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Test if all elements in list are of same type
res = False
x=test_list[0]
c=0
for ele in test_list:
    if(type(x)==type(ele)):
        c+=1
if(c==len(test_list)):
    res=True
     
# printing result
print ("Do all elements have same type : " + str(res))


Output

The original list is : [5, 6, 2, 5, 7, 9]
Do all elements have same type : True

Time Complexity: O(n)

Auxiliary Space: O(1)

Method #4: Using lambda function and filter() method

Python3




# Python3 code to demonstrate
# Test if all elements in list are of same type
 
# Initializing lists
test_list = [5, 6, 2, 5, 7, 9]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Test if all elements in list are of same type
res = False
first_type = type(test_list[0])
 
result = list(filter(lambda x: type(x) == first_type, test_list))
if(len(result) == len(test_list)):
    res = True
# printing result
print("Do all elements have same type : " + str(res))


Output

The original list is : [5, 6, 2, 5, 7, 9]
Do all elements have same type : True

Method #5:Using itertools.filterfalse() method

Python3




# Python3 code to demonstrate
# Test if all elements in list are of same type
import itertools
# Initializing lists
test_list = [5, 6, 2, 5, 7, 9]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Test if all elements in list are of same type
res = False
first_type = type(test_list[0])
 
result = list(itertools.filterfalse(lambda x: not type(x) == first_type, test_list))
if(len(result) == len(test_list)):
    res = True
# printing result
print("Do all elements have same type : " + str(res))


Output

The original list is : [5, 6, 2, 5, 7, 9]
Do all elements have same type : True

Time Complexity:O(N)
Auxiliary Space:O(N)

Method #6: Using set()

Python3




#Python3 code to demonstrate
#Test if all elements in list are of same type
#Using set
#Initializing lists
test_list = [5, 6, 2, 5, 7, 9]
 
#printing original list
print("The original list is : " + str(test_list))
 
#Test if all elements in list are of same type
#Using set
res = len(set(map(type, test_list))) == 1
 
#printing result
print("Do all elements have same type : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original list is : [5, 6, 2, 5, 7, 9]
Do all elements have same type : True

Time Complexity: O(n), n is the length of the input list.
Auxiliary Space: O(n), n is the length of the set created.

Method #7: Using numpy:

Algorithm:

  1. Create a list called test_list with some elements.
  2. Convert the list to a numpy array using np.array().
  3. Get the data type of the first element of the test_list using type(test_list[0]).
  4. Check if the data type of the numpy array is a sub-data type of the data type of the first element using np.issubdtype().
  5. Store the result of the check in a variable called res.

Python3




import numpy as np
#Initializing lists
test_list = [5, 6, 2, 5, 7, 9]
#printing original list
print("The original list is : " + str(test_list))
res = np.issubdtype(np.array(test_list).dtype, type(test_list[0]))
#printing result
print("Do all elements have same type : " + str(res))
#This code is contributed by Jyothi pinjala


Output:

The original list is : [5, 6, 2, 5, 7, 9]
Do all elements have same type : True

Time complexity:
The time complexity of this algorithm is O(n), where n is the length of the test_list. The np.array() function takes O(n) time to convert the list to a numpy array. The type() function takes O(1) time to get the data type of the first element. The np.issubdtype() function takes O(1) time to check if the data type of the numpy array is a sub-data type of the data type of the first element.

Space complexity:

The space complexity of this algorithm is O(n), where n is the length of the test_list. The np.array() function creates a new numpy array with the same elements as the list, which takes O(n) space. The type() function and np.issubdtype() function do not take any significant amount of space.

RELATED ARTICLES

Most Popular

Recent Comments