In this article we will dicuss different ways to check if all element in a given List are same or matches a condition.
Suppose we have a list of string i.e.
# List of string listOfStrings = ['Hello'] * 10
Now let’s use python all() function to check if all elements in the given list are same.
Python : all() function
Python all() function checks if all Elements of given Iterable is True.
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.
''' check if element are same using all() It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len(listOfStrings) > 0 : result = all(elem == listOfStrings[0] for elem in listOfStrings) if result : print("All Elements in List are Equal") else: print("All Elements in List are Not Equal")
Check if all elements are same using list.count()
count() returns the occurrence count of given element in the list.
Let’s call the count() function of list with firts element of list as argument. If its occurrence count is equal to the length of list, then it means all elements in list are Same i.e.
''' check if element are same using list.count() If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len(listOfStrings) > 0 : result = listOfStrings.count(listOfStrings[0]) == len(listOfStrings)
Let’s do the same thing in single line i.e.
result = len(listOfStrings) > 0 and all(elem == listOfStrings[0] for elem in listOfStrings)
Check if all elements are same using Set
As set contains only unique elements, so convert the list to set. If set size is 1 then it means all elements in given list are same i.e.
''' As set contains unique elements only, so if list has similar elements, then only one will stored in set. ''' result = len(set(listOfStrings)) == 1
Complete example is as follows,
def main(): # List of string listOfStrings = ['Hello'] * 10 # Print the List print(listOfStrings) ''' check if element are same using all() It will Iterate through all the elements in list and check if all elements are similar to first element or not. ''' result = False; if len(listOfStrings) > 0 : result = all(elem == listOfStrings[0] for elem in listOfStrings) if result : print("All Elements in List are Equal") else: print("All Elements in List are Not Equal") ''' check if element are same using list.count() If occurence count of first element in list is equal to length of list. Then it means all elements in List are equal ''' result = False; if len(listOfStrings) > 0 : result = listOfStrings.count(listOfStrings[0]) == len(listOfStrings) if result : print("All Elements in List are Equal") else: print("All Elements in List are Not Equal") # Do the above logic in single line result = len(listOfStrings) > 0 and all(elem == listOfStrings[0] for elem in listOfStrings) if result : print("All Elements in List are Equal") else: print("All Elements in List are Not Equal") ''' As set contains unique elements only, so if list has similar elements, then only one will stored in set. ''' result = len(set(listOfStrings)) == 1 if result : print("All Elements in List are Equal") else: print("All Elements in List are Not Equal") if __name__ == '__main__': main()
Output:
['Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello', 'Hello'] All Elements in List are Equal All Elements in List are Equal All Elements in List are Equal All Elements in List are Equal
Pandas Tutorials -Learn Data Analysis with Python
-
Pandas Tutorial Part #1 - Introduction to Data Analysis with Python
-
Pandas Tutorial Part #2 - Basics of Pandas Series
-
Pandas Tutorial Part #3 - Get & Set Series values
-
Pandas Tutorial Part #4 - Attributes & methods of Pandas Series
-
Pandas Tutorial Part #5 - Add or Remove Pandas Series elements
-
Pandas Tutorial Part #6 - Introduction to DataFrame
-
Pandas Tutorial Part #7 - DataFrame.loc[] - Select Rows / Columns by Indexing
-
Pandas Tutorial Part #8 - DataFrame.iloc[] - Select Rows / Columns by Label Names
-
Pandas Tutorial Part #9 - Filter DataFrame Rows
-
Pandas Tutorial Part #10 - Add/Remove DataFrame Rows & Columns
-
Pandas Tutorial Part #11 - DataFrame attributes & methods
-
Pandas Tutorial Part #12 - Handling Missing Data or NaN values
-
Pandas Tutorial Part #13 - Iterate over Rows & Columns of DataFrame
-
Pandas Tutorial Part #14 - Sorting DataFrame by Rows or Columns
-
Pandas Tutorial Part #15 - Merging or Concatenating DataFrames
-
Pandas Tutorial Part #16 - DataFrame GroupBy explained with examples
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.