
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python - Check if all elements in a list are identical
There may be occasions when a list will contain all the values which are same. In this article we will see various way to verify that.
With all
We use the all function to find the result of comparison of each element of the list with the first element. If each comparison gives a result of equality then the result is given as all elements are equal else all elements are not equal.
Example
listA = ['Sun', 'Sun', 'Mon'] resA = all(x == listA[0] for x in listA) if resA: print("in ListA all elements are same") else: print("In listA all elements are not same") listB = ['Sun', 'Sun', 'Sun'] resB = all(x == listA[0] for x in listB) if resB: print("In listB all elements are same") else: print("In listB all elements are not same")
Output
Running the above code gives us the following result −
In listA all elements are not same In listB all elements are same
With count
In this approach we count the number of occurrences of the first element and compare it with length of the elements in the list. If all elements are same then this length will match else it will not.
Example
listA = ['Sun', 'Sun', 'Mon'] resA = listA.count(listA[0]) == len(listA) if resA: print("in ListA all elements are same") else: print("In listA all elements are not same") listB = ['Sun', 'Sun', 'Sun'] resB = listB.count(listB[0]) == len(listB) if resB: print("In listB all elements are same") else: print("In listB all elements are not same")
Output
Running the above code gives us the following result −
In listA all elements are not same In listB all elements are same
- Related Articles
- Python - Check if all elements in a List are same
- Check if tuple and list are identical in Python
- Check if two list of tuples are identical in Python
- Check if list contains all unique elements in Python
- Check if two lists are identical in Python
- Check if all array elements are distinct in Python
- Python – Check if elements index are equal for list elements
- Python – Check if elements in a specific index are equal for list elements
- Python Program to check whether all elements in a string list are numeric
- Check if elements of Linked List are present in pair in Python
- Python Program to check if two given matrices are identical
- Python - Check if all the values in a list are less than a given value
- Check if all elements of the array are palindrome or not in Python
- Check if three consecutive elements in an array is identical in JavaScript
- Check if array elements are consecutive in Python

Advertisements