Given a list, write a Python program to check if all the elements in that list are identical.
Input : ['a', 'b', 'c'] Output : False Input : [1, 1, 1, 1] Output : True
Approach #1 : Using loop
Start a for loop and check if first element is identical to all other elements in the list. This approach takes O(n) time complexity.
Approach #2 : Using Set
Converting given list into set removes all duplicate elements, If the resultant set size is less than or equal to 1 then the list contains all identical elements.
Approach #3 : Using
By counting the number of times the first element occurs in the list, we can check if the count is equal to the size of list or not. In simple words, check if the first element is repeated throughout the list or not.
Approach #4 : Alternative method
Another method is to take the first element and multiply it with the length of given list to form a new list, So that the new list contains identical elements to first elements of given list size, and then compare it with the given list.
Approach #5 : Using extended slices
Python slice notation is used to retrieve a subset of values. Thus, we compare the start to end of the list to end to start of the list.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python List Equality | Program to check if two given matrices are identical
- Python | Check if tuple and list are identical
- Python | Check if two list of tuples are identical
- Python | Identical Consecutive Grouping in list
- Python | Check whether two lists are circularly identical
- Python | Check if two lists are identical
- Python - Consecutive identical elements count
- Python - Minimum identical consecutive Subarray
- Python | Pandas TimedeltaIndex.identical
- Python | Pandas Index.identical()
- Python | Identical Strings Grouping
- Python - Check List elements from Dictionary List
- Python - Check if elements index are equal for list elements
- Python | Check if all elements in a List are same
- Python | Check if list contains all unique elements
- Python | Check if all elements in list follow a condition
- Python | Convert list of string to list of list
- Python | Convert list of tuples to list of list
- Python | Convert List of String List to String List
- Python | Check if a nested list is a subset of another nested list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.