Python - Check if all elements in List are same
Last Updated :
10 Dec, 2024
To check if all items in list are same, we have multiple methods available in Python.
Using Set
Using set() is the best method to check if all items are same in list.
Python
a = [3, 3, 3, 3]
# Check if all elements are the same
result = len(set(a)) == 1
print(result)
Explanation:
- Converting given list to set will take O(n) time, because we are adding each item in set.
- In Set, checking length can be done in O(1) constant time.
Let's explore some other methods and see how we can check if all elements in a list are same or not.
Using all() method
all()
function checks if all elements in an iterable meet a condition. By comparing each element to the first list we can confirm if the entire list is uniform.
Python
#Driver Code Starts
a = [5, 5, 5, 5]
#Driver Code Ends
# Check if all elements are the same
result = all(x == a[0] for x in a)
#Driver Code Starts
print(result)
#Driver Code Ends
Using all() method can be preferred, as it stops iterating once it finds any mismatched item during comparison.
Explanation:
- x == a[0] for x in a : For each iteration, it checks if the current element x is equal to the first element of the list (a[0] which is 5 in this case)
- all() function takes above generator and checks if all values evaluates to True.
Using count() method
count() method will iterate over the entire list to count the occurrence of first item.
Python
a = [5, 5, 5, 5]
# count the occurence of first element
res = a.count(a[0]) == len(a)
print(res)
Explanation:
- a.count(a[0]) counts how many times the first element appears in the list.
- this result is compared with length of list using len(a)
- If both are same, above code will return True, else False.
Using For Loop
A simple loop iterates through the list and checks if all elements are equal to the first element. This approach is easy to understand and implement.
Python
a = [1, 1, 1, 1]
# Check if all elements are the same
result = True
for x in a:
if x != a[0]:
result = False
break
print( result)
Explanation:
- The loop compares each element to the first.
- If a mismatch is found the result is set to
False
and the loop exits
Using List Slicing
This approach compares all elements in the list to the first element using slicing. It’s concise and effective for short lists and comparing these ensures uniformity.
Python
#Driver Code Starts
a = [2, 2, 2, 2]
#Driver Code Ends
# Check if all elements are the same
result = a[1:] == a[:-1]
#Driver Code Starts
print(result)
#Driver Code Ends
Explanation:
a[1:]
contains all elements except the first.a[:-1]
contains all elements except the last.- If a[1:] and a[:-1] are same, it means all elements in the original list are the same.
Note: We should avoid using this method if the list could be empty or contain only one element, as it may give misleading results.
Similar Reads
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 using Python. Examples: Input : ['a', 'b', 'c'] Output : False Input : [1, 1, 1, 1] Output : TrueCheck if all elements in a list are identical or not using a loop Start a Python for loop and check if the f
5 min read
Python - Test if all elements in list are of same type When working with lists in Python, there are times when we need to ensure that all the elements in the list are of the same type or not. This is particularly useful in tasks like data analysis where uniformity is required for calculations. In this article, we will check several methods to perform th
2 min read
Test if all elements are present in list-Python The task of testing if all elements are present in a list in Python involves checking whether every item in a target list exists within a reference list. For example, given two lists a = [6, 4, 8, 9, 10] and b = [4, 6, 9], the task is to confirm that all elements in list b are also found in list a.U
3 min read
Python - Check if Kth index elements are unique Given a String list, check if all Kth index elements are unique. Input : test_list = ["gfg", "best", "for", "geeks"], K = 1 Output : False Explanation : e occurs as 1st index in both best and geeks.Input : test_list = ["gfg", "best", "geeks"], K = 2 Output : True Explanation : g, s, e, all are uniqu
5 min read
Check if any element in list satisfies a condition-Python The task of checking if any element in a list satisfies a condition involves iterating through the list and returning True if at least one element meets the condition otherwise, it returns False. For example, in a = [4, 5, 8, 9, 10, 17], checking ele > 10 returns True as 17 satisfies the conditio
2 min read