Home »
Python »
Python Programs
Check all elements of a list are the same or not in Python
Python | Check if all elements in a list are same: In this tutorial, we will learn how to check whether all elements in a list are the same or not. Learn with multiple approach and examples.
By IncludeHelp, on Last updated : July 05, 2023
Given a Python list, we have to check whether if all elements in a list are the same or not.
Consider the below example with sample input output:
Input:
List: [10, 10, 10, 10, 10]
Output:
Yes! All elements are the same.
Input:
List: [10, 20, 30, 40,50]
Output:
No! All elements are not the same.
How to check if all elements in a list are same?
There can be multiple approaches to check whether all elements in a list are same. We are discussing some of them which are:
- By checking each element
- By using the all() method
- By using list slicing
- By converting into set and checking the length
1. By checking each element
To check if all elements in a list are the same, you can iterate over the list and check first element with all elements of the list. If each element is equal to the first element, then returns True. False; otherwise.
Example
# function to check elements
def check_equal(list1):
result = True
first = list1[0]
for item in list1:
if first != item:
return False
break
return True
# lists
x = [10, 20, 30, 40, 50]
y = [10, 20, 20, 20, 20]
z = [10, 10, 10, 10, 10]
# printing lists
print("x:", x)
print("y:", y)
print("z:", z)
# checking elements
print("check_equal(x):", check_equal(x))
print("check_equal(y):", check_equal(y))
print("check_equal(z):", check_equal(z))
Output
x: [10, 20, 30, 40, 50]
y: [10, 20, 20, 20, 20]
z: [10, 10, 10, 10, 10]
check_equal(x): False
check_equal(y): False
check_equal(z): True
2. By using the all() method
By using the list comprehension and all() function, you can check whether if all elements in a list are the same or not. The all() function it used to check whether all elements of an iterable are True or not. It accepts an iterable container and returns True if all elements are True, else it returns False.
Example
# function to check elements
def check_equal(list1):
result = True
if len(list1) < 0:
result = False
result = all(item == list1[0] for item in list1)
return result
# lists
x = [10, 20, 30, 40, 50]
y = [10, 20, 20, 20, 20]
z = [10, 10, 10, 10, 10]
# printing lists
print("x:", x)
print("y:", y)
print("z:", z)
# checking elements
print("check_equal(x):", check_equal(x))
print("check_equal(y):", check_equal(y))
print("check_equal(z):", check_equal(z))
Output
x: [10, 20, 30, 40, 50]
y: [10, 20, 20, 20, 20]
z: [10, 10, 10, 10, 10]
check_equal(x): False
check_equal(y): False
check_equal(z): True
3. By using list slicing
You can also use use [1:] and [:-1] to compare all the elements in the given list to check that they are same or not.
Example
# function to check elements
def check_equal(list1):
return list1[1:] == list1[:-1]
# lists
x = [10, 20, 30, 40, 50]
y = [10, 20, 20, 20, 20]
z = [10, 10, 10, 10, 10]
# printing lists
print("x:", x)
print("y:", y)
print("z:", z)
# checking elements
print("check_equal(x):", check_equal(x))
print("check_equal(y):", check_equal(y))
print("check_equal(z):", check_equal(z))
Output
x: [10, 20, 30, 40, 50]
y: [10, 20, 20, 20, 20]
z: [10, 10, 10, 10, 10]
check_equal(x): False
check_equal(y): False
check_equal(z): True
4. By converting into set and checking the length
The another approach is to check if all elements are the same in list is that, you can convert the list to the set by using the set() method and then find the length of the set using the len() method. If length is equal to 1 then all elements are the same.
Example
# function to check elements
def check_equal(list1):
# converting to set and checking length
# if length is 1 then all items
# will be the same
return len(set(list1)) == 1
# lists
x = [10, 20, 30, 40, 50]
y = [10, 20, 20, 20, 20]
z = [10, 10, 10, 10, 10]
# printing lists
print("x:", x)
print("y:", y)
print("z:", z)
# checking elements
print("check_equal(x):", check_equal(x))
print("check_equal(y):", check_equal(y))
print("check_equal(z):", check_equal(z))
Output
x: [10, 20, 30, 40, 50]
y: [10, 20, 20, 20, 20]
z: [10, 10, 10, 10, 10]
check_equal(x): False
check_equal(y): False
check_equal(z): True
Python List Programs »