Python | Check if all elements in a list are identical
Last Updated :
17 May, 2023
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
def check( list ):
return all (i = = list [ 0 ] for i in list )
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
def check( list ):
return len ( set ( list )) = = 1
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 )
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
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 ]
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):
repeated = list (repeat(lst[ 0 ], len (lst)))
return repeated = = lst
print (check([ 'a' , 'b' , 'c' ]))
print (check([ 1 , 1 , 1 ]))
print (check([ 1 , 2 , 3 ]))
print (check([ 1 , 1 , 1 , 1 , 1 ]))
|
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))
|
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)
|
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.
Similar Reads
Python | Check if all elements in a List are same
To check if all items in list are same, we have multiple methods available in Python. Using SetUsing set() is the best method to check if all items are same in list. [GFGTABS] Python a = [3, 3, 3, 3] # Check if all elements are the same result = len(set(a)) == 1 print(result) [/GFGTABS]OutputTrue Ex
3 min read
Python - Check if elements index are equal for list elements
Given two lists and check list, test if for each element in check list, elements occur in similar index in 2 lists. Input : test_list1 = [2, 6, 9, 7, 8], test_list2 = [2, 7, 9, 4, 8], check_list = [9, 8, 7] Output : False Explanation : 7 is at 4th and 2nd place in both list, hence False. Input : tes
4 min read
Python | Check if all elements in list follow a condition
Sometimes, while working with Python list, we can have a problem in which we need to check if all the elements in list abide to a particular condition. This can have application in filtering in web development domain. Let's discuss certain ways in which this task can be performed. Method #1 : Using
5 min read
Python | Check if tuple and list are identical
Sometimes while working with different data in Python, we can have a problem of having data in different containers. In this situations, there can be need to test if data is identical cross containers. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop This is the
7 min read
Python - Check alternate peak elements in List
Given a list, the task is to write a Python program to test if it's alternate, i.e next and previous elements are either both smaller or larger across the whole list. Input : test_list = [2, 4, 1, 6, 4, 8, 0] Output : True Explanation : 4, 6, 8 are alternate and peaks (2 1). Input : test_list = [2,
3 min read
Test if all elements are present in list-Python
The task of testing if all elements are present in a list in Python involves checking whether every item in a target list exists within a reference list. For example, given two lists a = [6, 4, 8, 9, 10] and b = [4, 6, 9], the task is to confirm that all elements in list b are also found in list a.
3 min read
Python | Check if any element occurs n times in given list
Given a list, the task is to find whether any element occurs 'n' times in given list of integers. It will basically check for the first element that occurs n number of times. Examples: Input: l = [1, 2, 3, 4, 0, 4, 3, 2, 1, 2], n = 3 Output : 2 Input: l = [1, 2, 3, 4, 0, 4, 3, 2, 1, 2, 1, 1], n = 4
5 min read
Python program to check if any key has all the given list elements
Given a dictionary with list values and a list, the task is to write a Python program to check if any key has all the list elements. Examples: Input : test_dict = {'Gfg' : [5, 3, 1, 6, 4], 'is' : [8, 2, 1, 6, 4], 'best' : [1, 2, 7, 3, 9], 'for' : [5, 2, 7, 8, 4, 1], 'all' : [8, 5, 3, 1, 2]}, find_li
7 min read
Python | Check if two list of tuples are identical
Sometimes, while working with tuples, we can have a problem in which we have list of tuples and we need to test if they are exactly identical. This is a very basic problem and can occur in any domain. Let's discuss certain ways in which this task can be done. Method #1 : Using == operator This is th
4 min read
Python Program To Check If Two Linked Lists Are Identical
Two Linked Lists are identical when they have the same data and the arrangement of data is also the same. For example, Linked lists a (1->2->3) and b(1->2->3) are identical. . Write a function to check if the given two linked lists are identical. Recommended: Please solve it on "PRACTICE
4 min read