check if element are same using all[]
Let’s convert the list to Iterable and check if each entry of iterable is equal to first element of list using all[] i.e.
Advertisements
Python | Check if all elements in a List are same
Given a list, write a Python program to check if all the elements in given list are same.
Example:
Input: ['Geeks', 'Geeks', 'Geeks', 'Geeks', ] Output: Yes Input: ['Geeks', 'Is', 'all', 'Same', ] Output: NoThere are various ways we can do this task. Let’s see different ways we can check if all elements in a List are same.
Method #1: Comparing each element.
# Python program to check if all # ments in a List are same
def ckeckList[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', ] ckeckList[lst] |
Output:
Equal
But In Python, we can do the same task in much interesting ways.
Method #2: Using all[] method
# 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] |
Output:
Equal
Method #3: Using count[] method
# 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] |
Output:
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.
# 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"] |
Output:
EqualArticle Tags :
Python
Python Programs
Python list-programs
python-list
Practice Tags :
python-list
Read Full Article
Python | Check if all elements in a list are identical
Given a list, write a Python program to check if all the elements in that list are identical.
Examples:
Input : ['a', 'b', 'c'] Output : False Input : [1, 1, 1, 1] Output : TrueApproach #1 : Using loop
Start a for loop and check if first element is identical to all other elements in the list. This approach takes O[n] time complexity.
# 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
Approach #2 : Using Set
Converting given list into set removes all duplicate elements, If the resultant set size is less than or equal to 1 then the list contains all identical elements.
# Python3 program to check if # all elements in a list are identical def check[list]: return len[set[list]] Chủ Đề |