Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python - Check if all elements in a List are same
In Python, a list is a collection of ordered and mutable elements enclosed in square brackets []. Each element in a list has a unique position index, starting from 0. It can accept duplicate elements.
In some cases, we may need to check if all the elements in a list are identical or not to detect a special condition in algorithms. Let's go through the different methods to check if all elements in a list are the same or not.
Using set() Function
The Python set() function (built-in) creates an unordered collection of unique elements. To check if all elements in a list are the same, we convert the list into a set so that any duplicates are automatically removed. If the resulting set has only one element, all original elements were identical.
Example
Following is the example, in which we are using the set() function to check if all elements in a list are the same ?
my_list = [100, 100, 100, 100]
if len(set(my_list)) == 1:
print("All elements are the same.")
else:
print("Elements are not the same.")
Following is the output of the above example ?
All elements are the same.
Using a for Loop
A for loop in Python is used to iterate through elements of a data structure, such as a list, string, etc.
To verify all the elements are equal, we iterate through all the elements in the list and compare each element with the first element. If any element differs from the first, we know all elements are not the same.
Example
Following is an example, in which we are using a for loop to check if all elements in a list are the same ?
my_list = [100, 100, 100, 100]
first = my_list[0]
all_same = True
for item in my_list:
if item != first:
all_same = False
break
if all_same:
print("All elements are the same.")
else:
print("Elements are not the same.")
Here is the output of the above example ?
All elements are the same.
Using all() Function
The all() is a built-in function in Python that returns True if all elements in an iterable are true.
To check if all elements in a list are the same, we can use all() along with a generator expression to compare each item in the list with the first element. If any item is different, all() will return False, otherwise it returns True.
Example
Below is an example in which we are using the all() function to check if all elements in a list are the same ?
my_list = [100, 100, 100, 100]
if all(item == my_list[0] for item in my_list):
print("All elements are the same.")
else:
print("Elements are not the same.")
Below is the output of the above example ?
All elements are the same.
Comparison
| Method | Time Complexity | Best For |
|---|---|---|
set() |
O(n) | Simple and readable |
for loop |
O(n) - worst case | Early termination on difference |
all() |
O(n) - worst case | Pythonic and concise |
Conclusion
Use set() for simple readable code, all() for Pythonic style, or for loop when you need early termination. All methods efficiently determine if list elements are identical.
