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
# Python program to check if all # elements in a List are same def checkList(lst): ele = lst[ 0 ] chk = True # Comparing each element with first item for item in lst: if ele ! = item: chk = False break if (chk = = True ): print ( "Equal" ) else : print ( "Not equal" ) # Driver Code lst = [ 'Geeks' , 'Geeks' , 'Geeks' , 'Geeks' , ] checkList(lst) |
Equal
But In Python, we can do the same task in many interesting ways.
Method #2: Using all() method
Python3
# Python program to check if all # elements in a List are same 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" ) # Driver Code lst = [ 'Geeks' , 'Geeks' , 'Geeks' , 'Geeks' ] chkList(lst) |
Equal
Method #3: Using count() method
Python3
# Python program to check if all # elements in a List are same 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" ) # Driver Code lst = [ 'Geeks' , 'Geeks' , 'Geeks' , 'Geeks' ] chkList(lst) |
Equal
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
# Python program to check if all # elements in a List are same def chkList(lst): return len ( set (lst)) = = 1 # Driver Code lst = [ 'Geeks' , 'Geeks' , 'Geeks' , 'Geeks' ] if chkList(lst) = = True : print ( "Equal" ) else : print ( "Not Equal" ) |
Equal
Method #5 : Using len() method
Python3
# Python program to check if all # elements in a List are same lst = [ 'Geeks' , 'Geeks' , 'Geeks' , 'Geeks' ] if ([lst[ 0 ]] * len (lst) = = lst): print ( "Equal" ) else : print ( "Not equal" ) |
Equal
Method#6: Using recursion
Python3
#Python program to check if all the elements in the given list are the same. def checkList(lst, ele = None , index = 0 ): # if ele is None, assign the first element of the list to ele if ele is None : ele = lst[ 0 ] # base case: if index is equal to the length of the list, return True if index = = len (lst): return True # if the current element at index is not equal to ele, return False elif lst[index] ! = ele: return False # otherwise, call the function again with the next index else : return checkList(lst, ele, index + 1 ) # Driver Code lst = [ 'Geeks' , 'Geeks' , 'Geeks' , 'Geeks' ] # check if the function returns True if checkList(lst): print ( "Equal" ) else : print ( "Not equal" ) #This code is contributed Vinay Pinjala. |
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) # driver code 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)
Please Login to comment...