Python | Check if all elements in a List are same
Given a list, write a Python program to check if all the elements in the given list are the same.
Example:
Input: ['Geeks', 'Geeks', 'Geeks', 'Geeks', ]
Output: Yes
Input: ['Geeks', 'Is', 'all', 'Same', ]
Output: No
There are various ways we can do this task. Let’s see different ways we can check if all elements in a List are the same.
Method #1: Comparing each element.
Python3
def checkList(lst):
ele = lst[ 0 ]
chk = True
for item in lst:
if ele ! = item:
chk = False
break
if (chk = = True ):
print ( "Equal" )
else :
print ( "Not equal" )
lst = [ 'Geeks' , 'Geeks' , 'Geeks' , 'Geeks' , ]
checkList(lst)
|
But In Python, we can do the same task in many interesting ways.
Method #2: Using all() method
Python3
res = False
def chkList(lst):
if len (lst) < 0 :
res = True
res = all (ele = = lst[ 0 ] for ele in lst)
if (res):
print ( "Equal" )
else :
print ( "Not equal" )
lst = [ 'Geeks' , 'Geeks' , 'Geeks' , 'Geeks' ]
chkList(lst)
|
Method #3: Using count() method
Python3
res = False
def chkList(lst):
if len (lst) < 0 :
res = True
res = lst.count(lst[ 0 ]) = = len (lst)
if (res):
print ( "Equal" )
else :
print ( "Not equal" )
lst = [ 'Geeks' , 'Geeks' , 'Geeks' , 'Geeks' ]
chkList(lst)
|
Method #4: Using set data structure Since we know there cannot be duplicate elements in a set, we can use this property to check whether all the elements are same or not.
Python3
def chkList(lst):
return len ( set (lst)) = = 1
lst = [ 'Geeks' , 'Geeks' , 'Geeks' , 'Geeks' ]
if chkList(lst) = = True :
print ( "Equal" )
else :
print ( "Not Equal" )
|
Method #5 : Using len() method
Python3
lst = [ 'Geeks' , 'Geeks' , 'Geeks' , 'Geeks' ]
if ([lst[ 0 ]] * len (lst) = = lst):
print ( "Equal" )
else :
print ( "Not equal" )
|
Method#6: Using recursion
Python3
def checkList(lst, ele = None , index = 0 ):
if ele is None :
ele = lst[ 0 ]
if index = = len (lst):
return True
elif lst[index] ! = ele:
return False
else :
return checkList(lst, ele, index + 1 )
lst = [ 'Geeks' , 'Geeks' , 'Geeks' , 'Geeks' ]
if checkList(lst):
print ( "Equal" )
else :
print ( "Not equal" )
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #7: Using the reduce function from the functools library and the operator module:
Python3
from functools import reduce
import operator
def all_elements_same(lst):
return reduce (operator.eq, lst)
lst = [ 1 , 1 , 1 , 1 , 1 ]
if all_elements_same(lst):
print ( "Equal" )
else :
print ( "Not Equal" )
|
Output:
Equal
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #8 : Using operator.countOf() method
- Check whether count of first element is equal to length of list using operator.countOf()
- If True then all elements of list are equal Display Equal
- If False then Display Not equal
Python3
import operator
res = False
def chkList(lst):
if len (lst) < 0 :
res = True
res = operator.countOf(lst, lst[ 0 ]) = = len (lst)
if (res):
print ( "Equal" )
else :
print ( "Not equal" )
lst = [ 'Geeks' , 'Geeks' , 'Geeks' , 'Geeks' ]
chkList(lst)
|
Time Complexity: O(n) n – length of list
Auxiliary Space: O(1)
Method #9: Using heapq module:
- Import the heapq module.
- Define the all_elements_same function which takes a list as an argument.
- Use the nsmallest function from heapq to find the smallest len(lst) elements in the list and check if all the elements found are equal to the first element of the list using all() functions. If it is True, print “Equal“, else print “Not Equal“.
Below is the implementation of the above approach:
Python3
import heapq
def all_elements_same(lst):
return all (x = = lst[ 0 ] for x in heapq.nsmallest( len (lst), lst))
lst = [ 1 , 1 , 1 , 1 , 1 ]
if all_elements_same(lst):
print ( "Equal" )
else :
print ( "Not Equal" )
|
Time Complexity: The time complexity of this algorithm is O(N log N), where N is the length of the input list. This is because the nsmallest function uses a heap to find the smallest elements, which has a time complexity of O(N log N).
Space Complexity: The space complexity of this algorithm is O(N), where N is the length of the input list. This is because the nsmallest function creates a heap of size n to store the smallest elements. Additionally, the generator expression used in all functions also requires O(N) space.
Last Updated :
05 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...