Python | Check if all elements in a list are identical
Last Updated :
05 Jul, 2025
Checking if all elements in a list are identical means verifying that every item has the same value. It's useful for ensuring data consistency and can be done easily using Python's built-in features.
Examples:
Input: ['a', 'b', 'c']
Output: False
Input: [1, 1, 1, 1]
Output: True
Let's explore different methods to check it.
Using set()
set() function removes duplicate values from a list. So, if all elements in a list are identical, converting it to a set will result in a set with only one element.
Python
li = [1, 1, 1]
res = len(set(li)) == 1
print(res)
Explanation: len(set(lst)) == 1 checks if the list has only one unique element.
Using all() with generator expression
all() function along with a generator expression compares each element in a list to the first one. If all comparisons return True, it means all elements in the list are identical.
Python
li = [1, 1, 1]
res = all(x == li[0] for x in li)
print(res)
Explanation: checks if all elements in the list li are equal to the first element li[0] using all() with a generator expression.
Using count()
count() method checks if all elements are identical by verifying if the first element appears as many times as the length of the list.
Python
li = [1, 1, 1]
res = li.count(li[0]) == len(li)
print(res)
Explanation: li.count(li[0]) == len(li) checks if the first element appears as many times as the total number of elements.
Using list multiplication
It compares the original list with a new list created by repeating the first element len(list) times. If both lists are equal, it means all elements in the original list are identical.
Python
li = [1, 1, 1]
res = li == [li[0]] * len(li)
print(res)
Explanation: li == [li[0]] * len(li) checks if the list is equal to a new list made by repeating the first element.
Using map() + lambda + all()
lambda function is applied using map() to compare each element in the list to the first one. The all() function then checks if all comparisons are True, indicating that all elements in the list are identical.
Python
li = [1, 1, 1]
res = all(map(lambda x: x == li[0], li))
print(res)
Explanation: all(map(lambda x: x == li[0], li)) checks if every element in the list is equal to the first element using a lambda function, confirming all elements are identical.
Using loop
It uses a for loop to compare each element in the list with the first element. If any element is different, it returns False. If the loop completes without finding a mismatch, it means all elements are identical.
Python
li = [1, 1, 1]
res = True
for x in li:
if x != li[0]:
res = False
break
print(res)
Explanation: loop compares each element to the first one and if any element is different, it sets res to False, meaning not all elements are identical.
Related Articles:
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice