Open In App

Python | Check if all elements in a list are identical

Last Updated : 17 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Below is the code to implement the above 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


Output

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.

Approach#6: Using the itertools library

this approach uses the itertools library’s groupby() function to group the elements in the list by identity. If the resulting groups contain only one group, then all elements in the list are identical.

Algorithm

1. Import the itertools library
2. Use the groupby() function to group the elements in the list by identity
3. If the resulting groups contain only one group, then all elements in the list are identical
4. Return the result

Python3




import itertools
 
 
def check_identical(lst):
    groups = itertools.groupby(lst)
    return len(list(groups)) == 1
 
 
lst = ['a', 'b', 'c']
print(check_identical(lst))
 
lst = [1, 1, 1]
print(check_identical(lst))


Output

False

Time Complexity: O(n), where n is len of list.
Space Complexity: O(n), where n is len of list.

Approach#7: Uaing lambda

Using map() with an anonymous function to apply a comparison between the first element of the list and each of its elements, and then using all() to check if all comparisons return True.

Algorithm

1. Create a list of elements called data.
2. Create an anonymous function that compares its argument to the first element of data.
3. Use map() to apply the anonymous function to all elements of data.
4. Use all() to check if all the results of the map() function are True.
5. Print the result of the all() function.

Python3




data = [1,1,1,1]
result = all(map(lambda x: x == data[0], data))
print(result)


Output

True

Time Complexity: O(n), where n is the length of the list, because we have to iterate over the list once to compare all its elements or to create the set.

Auxiliary Space: O(n), where n is the length of the list, because we are creating an anonymous function and either a map or a set that contains all the elements of the list.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads