Python | Check if all elements in a list are identical
Given a list, write a Python program to check if all the elements in that list are identical using Python.
Examples:
Input : ['a', 'b', 'c'] Output : False Input : [1, 1, 1, 1] Output : True
Check if all elements in a list are identical or not using a loop
Start a Python for loop and check if the first element is identical to all other elements in the list. This approach takes O(n) time complexity.
Python3
# Python3 program to check if # all elements in a list are identical def check( list ): return all (i = = list [ 0 ] for i in list ) # Driver code print (check([ 'a' , 'b' , 'c' ])) print (check([ 1 , 1 , 1 ])) |
Output:
False True
Check if all elements in a list are identical or not using set()
Set Converting the given list into Python set. It removes all duplicate elements, If the resultant set size is less than or equal to 1 then the list contains all identical elements.
Python3
# Python3 program to check if # all elements in a list are identical def check( list ): return len ( set ( list )) = = 1 # Driver code print (check([ 'a' , 'b' , 'c' ])) print (check([ 1 , 1 , 1 ])) |
Output:
False True
Check if all elements in a list are identical or not using count()
By counting the number of times the first element occurs in the list, we can check if the count is equal to the size of the list or not. In simple words, check if the first element is repeated throughout the list or not.
Python3
def check( list ): return list .count( list [ 0 ]) = = len ( list ) # Driver code print (check([ 'a' , 'b' , 'c' ])) print (check([ 1 , 1 , 1 ])) |
Output:
False True
Check if all elements in a list are identical or not using the length
Another method is to take the first element and multiply it by the length of the given list to form a new list, So that the new list contains identical elements to the first elements of the given list size, and then compare it with the given list.
Python3
def check(x): return x and [x[ 0 ]] * len (x) = = x # Driver code print (check([ 'a' , 'b' , 'c' ])) print (check([ 1 , 1 , 1 ])) |
Output:
False True
Check if all elements in a list are identical or not using slicing
Python slice notation is used to retrieve a subset of values. Thus, we compare the start to the end of the list to the end to the start of the list.
Python3
def check( list ): return list [ 1 :] = = list [: - 1 ] # Driver code print (check([ 'a' , 'b' , 'c' ])) print (check([ 1 , 1 , 1 ])) |
Output:
False True
Check if all elements in a list are identical or not using itertools
Here is another approach that you can use to check if all the elements in a list are identical, using the repeat() function from the itertools module:
- Import the repeat() function from the itertools module.
- Use the repeat() function to generate an iterator that returns the first element of the list repeated len(lst) times.
- Convert the iterator to a list and compare it to the original list. If the lists are equal, return True, otherwise return False.
Here is an example of how you can implement this approach:
Python3
from itertools import repeat def check(lst): # Use the repeat function to generate an iterator that returns the first element of the list repeated len(lst) times repeated = list (repeat(lst[ 0 ], len (lst))) # Compare the repeated list to the original list and return the result return repeated = = lst # Test the function with different input lists print (check([ 'a' , 'b' , 'c' ])) print (check([ 1 , 1 , 1 ])) print (check([ 1 , 2 , 3 ])) print (check([ 1 , 1 , 1 , 1 , 1 ])) #This code is contributed by Edula Vinay Kumar Reddy |
False True False True
This approach has a time complexity of O(n) in the worst case, where n is the length of the list, because it involves iterating over the elements in the list and comparing them to the first element. The space complexity is O(n) because it involves creating a new list with a size proportional to the number of elements in the original list.
Please Login to comment...