Python Check if all elements are same using list.count().
Description:
count() method is an inbuilt method in python which count the number of occurrences of a sub string in a given string.
So the idea to check all numbers are same using count is :
Check the result of count() method and compare it with the length of the list, if all the elements in the list have same then it will be equal to the count() method result.
Program:
#set a pointer to check
res = FalsedefcheckList(myList):if len(myList) < 0 :
res = True
res = myList.count(myList[0]) ==len(myList)
if(res):
print("Equal")
else:
print("Not equal")
# Driver Code
myList = [2,2,3,3,2,2,2]
checkList(myList)
Comments